mac_esp.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641
  1. /* mac_esp.c: ESP front-end for Macintosh Quadra systems.
  2. *
  3. * Adapted from jazz_esp.c and the old mac_esp.c.
  4. *
  5. * The pseudo DMA algorithm is based on the one used in NetBSD.
  6. * See sys/arch/mac68k/obio/esp.c for some background information.
  7. *
  8. * Copyright (C) 2007-2008 Finn Thain
  9. */
  10. #include <linux/kernel.h>
  11. #include <linux/types.h>
  12. #include <linux/module.h>
  13. #include <linux/init.h>
  14. #include <linux/interrupt.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/dma-mapping.h>
  17. #include <linux/scatterlist.h>
  18. #include <linux/delay.h>
  19. #include <linux/io.h>
  20. #include <linux/nubus.h>
  21. #include <linux/slab.h>
  22. #include <asm/irq.h>
  23. #include <asm/dma.h>
  24. #include <asm/macints.h>
  25. #include <asm/macintosh.h>
  26. #include <asm/mac_via.h>
  27. #include <scsi/scsi_host.h>
  28. #include "esp_scsi.h"
  29. #define DRV_MODULE_NAME "mac_esp"
  30. #define PFX DRV_MODULE_NAME ": "
  31. #define DRV_VERSION "1.000"
  32. #define DRV_MODULE_RELDATE "Sept 15, 2007"
  33. #define MAC_ESP_IO_BASE 0x50F00000
  34. #define MAC_ESP_REGS_QUADRA (MAC_ESP_IO_BASE + 0x10000)
  35. #define MAC_ESP_REGS_QUADRA2 (MAC_ESP_IO_BASE + 0xF000)
  36. #define MAC_ESP_REGS_QUADRA3 (MAC_ESP_IO_BASE + 0x18000)
  37. #define MAC_ESP_REGS_SPACING 0x402
  38. #define MAC_ESP_PDMA_REG 0xF9800024
  39. #define MAC_ESP_PDMA_REG_SPACING 0x4
  40. #define MAC_ESP_PDMA_IO_OFFSET 0x100
  41. #define esp_read8(REG) mac_esp_read8(esp, REG)
  42. #define esp_write8(VAL, REG) mac_esp_write8(esp, VAL, REG)
  43. struct mac_esp_priv {
  44. struct esp *esp;
  45. void __iomem *pdma_regs;
  46. void __iomem *pdma_io;
  47. int error;
  48. };
  49. static struct esp *esp_chips[2];
  50. #define MAC_ESP_GET_PRIV(esp) ((struct mac_esp_priv *) \
  51. platform_get_drvdata((struct platform_device *) \
  52. (esp->dev)))
  53. static inline void mac_esp_write8(struct esp *esp, u8 val, unsigned long reg)
  54. {
  55. nubus_writeb(val, esp->regs + reg * 16);
  56. }
  57. static inline u8 mac_esp_read8(struct esp *esp, unsigned long reg)
  58. {
  59. return nubus_readb(esp->regs + reg * 16);
  60. }
  61. /* For pseudo DMA and PIO we need the virtual address
  62. * so this address mapping is the identity mapping.
  63. */
  64. static dma_addr_t mac_esp_map_single(struct esp *esp, void *buf,
  65. size_t sz, int dir)
  66. {
  67. return (dma_addr_t)buf;
  68. }
  69. static int mac_esp_map_sg(struct esp *esp, struct scatterlist *sg,
  70. int num_sg, int dir)
  71. {
  72. int i;
  73. for (i = 0; i < num_sg; i++)
  74. sg[i].dma_address = (u32)sg_virt(&sg[i]);
  75. return num_sg;
  76. }
  77. static void mac_esp_unmap_single(struct esp *esp, dma_addr_t addr,
  78. size_t sz, int dir)
  79. {
  80. /* Nothing to do. */
  81. }
  82. static void mac_esp_unmap_sg(struct esp *esp, struct scatterlist *sg,
  83. int num_sg, int dir)
  84. {
  85. /* Nothing to do. */
  86. }
  87. static void mac_esp_reset_dma(struct esp *esp)
  88. {
  89. /* Nothing to do. */
  90. }
  91. static void mac_esp_dma_drain(struct esp *esp)
  92. {
  93. /* Nothing to do. */
  94. }
  95. static void mac_esp_dma_invalidate(struct esp *esp)
  96. {
  97. /* Nothing to do. */
  98. }
  99. static int mac_esp_dma_error(struct esp *esp)
  100. {
  101. return MAC_ESP_GET_PRIV(esp)->error;
  102. }
  103. static inline int mac_esp_wait_for_empty_fifo(struct esp *esp)
  104. {
  105. struct mac_esp_priv *mep = MAC_ESP_GET_PRIV(esp);
  106. int i = 500000;
  107. do {
  108. if (!(esp_read8(ESP_FFLAGS) & ESP_FF_FBYTES))
  109. return 0;
  110. if (esp_read8(ESP_STATUS) & ESP_STAT_INTR)
  111. return 1;
  112. udelay(2);
  113. } while (--i);
  114. printk(KERN_ERR PFX "FIFO is not empty (sreg %02x)\n",
  115. esp_read8(ESP_STATUS));
  116. mep->error = 1;
  117. return 1;
  118. }
  119. static inline int mac_esp_wait_for_dreq(struct esp *esp)
  120. {
  121. struct mac_esp_priv *mep = MAC_ESP_GET_PRIV(esp);
  122. int i = 500000;
  123. do {
  124. if (mep->pdma_regs == NULL) {
  125. if (via2_scsi_drq_pending())
  126. return 0;
  127. } else {
  128. if (nubus_readl(mep->pdma_regs) & 0x200)
  129. return 0;
  130. }
  131. if (esp_read8(ESP_STATUS) & ESP_STAT_INTR)
  132. return 1;
  133. udelay(2);
  134. } while (--i);
  135. printk(KERN_ERR PFX "PDMA timeout (sreg %02x)\n",
  136. esp_read8(ESP_STATUS));
  137. mep->error = 1;
  138. return 1;
  139. }
  140. #define MAC_ESP_PDMA_LOOP(operands) \
  141. asm volatile ( \
  142. " tstw %1 \n" \
  143. " jbeq 20f \n" \
  144. "1: movew " operands " \n" \
  145. "2: movew " operands " \n" \
  146. "3: movew " operands " \n" \
  147. "4: movew " operands " \n" \
  148. "5: movew " operands " \n" \
  149. "6: movew " operands " \n" \
  150. "7: movew " operands " \n" \
  151. "8: movew " operands " \n" \
  152. "9: movew " operands " \n" \
  153. "10: movew " operands " \n" \
  154. "11: movew " operands " \n" \
  155. "12: movew " operands " \n" \
  156. "13: movew " operands " \n" \
  157. "14: movew " operands " \n" \
  158. "15: movew " operands " \n" \
  159. "16: movew " operands " \n" \
  160. " subqw #1,%1 \n" \
  161. " jbne 1b \n" \
  162. "20: tstw %2 \n" \
  163. " jbeq 30f \n" \
  164. "21: movew " operands " \n" \
  165. " subqw #1,%2 \n" \
  166. " jbne 21b \n" \
  167. "30: tstw %3 \n" \
  168. " jbeq 40f \n" \
  169. "31: moveb " operands " \n" \
  170. "32: nop \n" \
  171. "40: \n" \
  172. " \n" \
  173. " .section __ex_table,\"a\" \n" \
  174. " .align 4 \n" \
  175. " .long 1b,40b \n" \
  176. " .long 2b,40b \n" \
  177. " .long 3b,40b \n" \
  178. " .long 4b,40b \n" \
  179. " .long 5b,40b \n" \
  180. " .long 6b,40b \n" \
  181. " .long 7b,40b \n" \
  182. " .long 8b,40b \n" \
  183. " .long 9b,40b \n" \
  184. " .long 10b,40b \n" \
  185. " .long 11b,40b \n" \
  186. " .long 12b,40b \n" \
  187. " .long 13b,40b \n" \
  188. " .long 14b,40b \n" \
  189. " .long 15b,40b \n" \
  190. " .long 16b,40b \n" \
  191. " .long 21b,40b \n" \
  192. " .long 31b,40b \n" \
  193. " .long 32b,40b \n" \
  194. " .previous \n" \
  195. : "+a" (addr), "+r" (count32), "+r" (count2) \
  196. : "g" (count1), "a" (mep->pdma_io))
  197. static void mac_esp_send_pdma_cmd(struct esp *esp, u32 addr, u32 esp_count,
  198. u32 dma_count, int write, u8 cmd)
  199. {
  200. struct mac_esp_priv *mep = MAC_ESP_GET_PRIV(esp);
  201. mep->error = 0;
  202. if (!write)
  203. scsi_esp_cmd(esp, ESP_CMD_FLUSH);
  204. esp_write8((esp_count >> 0) & 0xFF, ESP_TCLOW);
  205. esp_write8((esp_count >> 8) & 0xFF, ESP_TCMED);
  206. scsi_esp_cmd(esp, cmd);
  207. do {
  208. unsigned int count32 = esp_count >> 5;
  209. unsigned int count2 = (esp_count & 0x1F) >> 1;
  210. unsigned int count1 = esp_count & 1;
  211. unsigned int start_addr = addr;
  212. if (mac_esp_wait_for_dreq(esp))
  213. break;
  214. if (write) {
  215. MAC_ESP_PDMA_LOOP("%4@,%0@+");
  216. esp_count -= addr - start_addr;
  217. } else {
  218. unsigned int n;
  219. MAC_ESP_PDMA_LOOP("%0@+,%4@");
  220. if (mac_esp_wait_for_empty_fifo(esp))
  221. break;
  222. n = (esp_read8(ESP_TCMED) << 8) + esp_read8(ESP_TCLOW);
  223. addr = start_addr + esp_count - n;
  224. esp_count = n;
  225. }
  226. } while (esp_count);
  227. }
  228. /*
  229. * Programmed IO routines follow.
  230. */
  231. static inline unsigned int mac_esp_wait_for_fifo(struct esp *esp)
  232. {
  233. int i = 500000;
  234. do {
  235. unsigned int fbytes = esp_read8(ESP_FFLAGS) & ESP_FF_FBYTES;
  236. if (fbytes)
  237. return fbytes;
  238. udelay(2);
  239. } while (--i);
  240. printk(KERN_ERR PFX "FIFO is empty (sreg %02x)\n",
  241. esp_read8(ESP_STATUS));
  242. return 0;
  243. }
  244. static inline int mac_esp_wait_for_intr(struct esp *esp)
  245. {
  246. struct mac_esp_priv *mep = MAC_ESP_GET_PRIV(esp);
  247. int i = 500000;
  248. do {
  249. esp->sreg = esp_read8(ESP_STATUS);
  250. if (esp->sreg & ESP_STAT_INTR)
  251. return 0;
  252. udelay(2);
  253. } while (--i);
  254. printk(KERN_ERR PFX "IRQ timeout (sreg %02x)\n", esp->sreg);
  255. mep->error = 1;
  256. return 1;
  257. }
  258. #define MAC_ESP_PIO_LOOP(operands, reg1) \
  259. asm volatile ( \
  260. "1: moveb " operands " \n" \
  261. " subqw #1,%1 \n" \
  262. " jbne 1b \n" \
  263. : "+a" (addr), "+r" (reg1) \
  264. : "a" (fifo))
  265. #define MAC_ESP_PIO_FILL(operands, reg1) \
  266. asm volatile ( \
  267. " moveb " operands " \n" \
  268. " moveb " operands " \n" \
  269. " moveb " operands " \n" \
  270. " moveb " operands " \n" \
  271. " moveb " operands " \n" \
  272. " moveb " operands " \n" \
  273. " moveb " operands " \n" \
  274. " moveb " operands " \n" \
  275. " moveb " operands " \n" \
  276. " moveb " operands " \n" \
  277. " moveb " operands " \n" \
  278. " moveb " operands " \n" \
  279. " moveb " operands " \n" \
  280. " moveb " operands " \n" \
  281. " moveb " operands " \n" \
  282. " moveb " operands " \n" \
  283. " subqw #8,%1 \n" \
  284. " subqw #8,%1 \n" \
  285. : "+a" (addr), "+r" (reg1) \
  286. : "a" (fifo))
  287. #define MAC_ESP_FIFO_SIZE 16
  288. static void mac_esp_send_pio_cmd(struct esp *esp, u32 addr, u32 esp_count,
  289. u32 dma_count, int write, u8 cmd)
  290. {
  291. struct mac_esp_priv *mep = MAC_ESP_GET_PRIV(esp);
  292. u8 *fifo = esp->regs + ESP_FDATA * 16;
  293. cmd &= ~ESP_CMD_DMA;
  294. mep->error = 0;
  295. if (write) {
  296. scsi_esp_cmd(esp, cmd);
  297. while (1) {
  298. unsigned int n;
  299. n = mac_esp_wait_for_fifo(esp);
  300. if (!n)
  301. break;
  302. if (n > esp_count)
  303. n = esp_count;
  304. esp_count -= n;
  305. MAC_ESP_PIO_LOOP("%2@,%0@+", n);
  306. if (!esp_count)
  307. break;
  308. if (mac_esp_wait_for_intr(esp))
  309. break;
  310. if (((esp->sreg & ESP_STAT_PMASK) != ESP_DIP) &&
  311. ((esp->sreg & ESP_STAT_PMASK) != ESP_MIP))
  312. break;
  313. esp->ireg = esp_read8(ESP_INTRPT);
  314. if ((esp->ireg & (ESP_INTR_DC | ESP_INTR_BSERV)) !=
  315. ESP_INTR_BSERV)
  316. break;
  317. scsi_esp_cmd(esp, ESP_CMD_TI);
  318. }
  319. } else {
  320. scsi_esp_cmd(esp, ESP_CMD_FLUSH);
  321. if (esp_count >= MAC_ESP_FIFO_SIZE)
  322. MAC_ESP_PIO_FILL("%0@+,%2@", esp_count);
  323. else
  324. MAC_ESP_PIO_LOOP("%0@+,%2@", esp_count);
  325. scsi_esp_cmd(esp, cmd);
  326. while (esp_count) {
  327. unsigned int n;
  328. if (mac_esp_wait_for_intr(esp))
  329. break;
  330. if (((esp->sreg & ESP_STAT_PMASK) != ESP_DOP) &&
  331. ((esp->sreg & ESP_STAT_PMASK) != ESP_MOP))
  332. break;
  333. esp->ireg = esp_read8(ESP_INTRPT);
  334. if ((esp->ireg & (ESP_INTR_DC | ESP_INTR_BSERV)) !=
  335. ESP_INTR_BSERV)
  336. break;
  337. n = MAC_ESP_FIFO_SIZE -
  338. (esp_read8(ESP_FFLAGS) & ESP_FF_FBYTES);
  339. if (n > esp_count)
  340. n = esp_count;
  341. if (n == MAC_ESP_FIFO_SIZE) {
  342. MAC_ESP_PIO_FILL("%0@+,%2@", esp_count);
  343. } else {
  344. esp_count -= n;
  345. MAC_ESP_PIO_LOOP("%0@+,%2@", n);
  346. }
  347. scsi_esp_cmd(esp, ESP_CMD_TI);
  348. }
  349. }
  350. }
  351. static int mac_esp_irq_pending(struct esp *esp)
  352. {
  353. if (esp_read8(ESP_STATUS) & ESP_STAT_INTR)
  354. return 1;
  355. return 0;
  356. }
  357. static u32 mac_esp_dma_length_limit(struct esp *esp, u32 dma_addr, u32 dma_len)
  358. {
  359. return dma_len > 0xFFFF ? 0xFFFF : dma_len;
  360. }
  361. static irqreturn_t mac_scsi_esp_intr(int irq, void *dev_id)
  362. {
  363. int got_intr;
  364. /*
  365. * This is an edge triggered IRQ, so we have to be careful to
  366. * avoid missing a transition when it is shared by two ESP devices.
  367. */
  368. do {
  369. got_intr = 0;
  370. if (esp_chips[0] &&
  371. (mac_esp_read8(esp_chips[0], ESP_STATUS) & ESP_STAT_INTR)) {
  372. (void)scsi_esp_intr(irq, esp_chips[0]);
  373. got_intr = 1;
  374. }
  375. if (esp_chips[1] &&
  376. (mac_esp_read8(esp_chips[1], ESP_STATUS) & ESP_STAT_INTR)) {
  377. (void)scsi_esp_intr(irq, esp_chips[1]);
  378. got_intr = 1;
  379. }
  380. } while (got_intr);
  381. return IRQ_HANDLED;
  382. }
  383. static struct esp_driver_ops mac_esp_ops = {
  384. .esp_write8 = mac_esp_write8,
  385. .esp_read8 = mac_esp_read8,
  386. .map_single = mac_esp_map_single,
  387. .map_sg = mac_esp_map_sg,
  388. .unmap_single = mac_esp_unmap_single,
  389. .unmap_sg = mac_esp_unmap_sg,
  390. .irq_pending = mac_esp_irq_pending,
  391. .dma_length_limit = mac_esp_dma_length_limit,
  392. .reset_dma = mac_esp_reset_dma,
  393. .dma_drain = mac_esp_dma_drain,
  394. .dma_invalidate = mac_esp_dma_invalidate,
  395. .send_dma_cmd = mac_esp_send_pdma_cmd,
  396. .dma_error = mac_esp_dma_error,
  397. };
  398. static int esp_mac_probe(struct platform_device *dev)
  399. {
  400. struct scsi_host_template *tpnt = &scsi_esp_template;
  401. struct Scsi_Host *host;
  402. struct esp *esp;
  403. int err;
  404. struct mac_esp_priv *mep;
  405. if (!MACH_IS_MAC)
  406. return -ENODEV;
  407. if (dev->id > 1)
  408. return -ENODEV;
  409. host = scsi_host_alloc(tpnt, sizeof(struct esp));
  410. err = -ENOMEM;
  411. if (!host)
  412. goto fail;
  413. host->max_id = 8;
  414. host->use_clustering = DISABLE_CLUSTERING;
  415. esp = shost_priv(host);
  416. esp->host = host;
  417. esp->dev = dev;
  418. esp->command_block = kzalloc(16, GFP_KERNEL);
  419. if (!esp->command_block)
  420. goto fail_unlink;
  421. esp->command_block_dma = (dma_addr_t)esp->command_block;
  422. esp->scsi_id = 7;
  423. host->this_id = esp->scsi_id;
  424. esp->scsi_id_mask = 1 << esp->scsi_id;
  425. mep = kzalloc(sizeof(struct mac_esp_priv), GFP_KERNEL);
  426. if (!mep)
  427. goto fail_free_command_block;
  428. mep->esp = esp;
  429. platform_set_drvdata(dev, mep);
  430. switch (macintosh_config->scsi_type) {
  431. case MAC_SCSI_QUADRA:
  432. esp->cfreq = 16500000;
  433. esp->regs = (void __iomem *)MAC_ESP_REGS_QUADRA;
  434. mep->pdma_io = esp->regs + MAC_ESP_PDMA_IO_OFFSET;
  435. mep->pdma_regs = NULL;
  436. break;
  437. case MAC_SCSI_QUADRA2:
  438. esp->cfreq = 25000000;
  439. esp->regs = (void __iomem *)(MAC_ESP_REGS_QUADRA2 +
  440. dev->id * MAC_ESP_REGS_SPACING);
  441. mep->pdma_io = esp->regs + MAC_ESP_PDMA_IO_OFFSET;
  442. mep->pdma_regs = (void __iomem *)(MAC_ESP_PDMA_REG +
  443. dev->id * MAC_ESP_PDMA_REG_SPACING);
  444. nubus_writel(0x1d1, mep->pdma_regs);
  445. break;
  446. case MAC_SCSI_QUADRA3:
  447. /* These quadras have a real DMA controller (the PSC) but we
  448. * don't know how to drive it so we must use PIO instead.
  449. */
  450. esp->cfreq = 25000000;
  451. esp->regs = (void __iomem *)MAC_ESP_REGS_QUADRA3;
  452. mep->pdma_io = NULL;
  453. mep->pdma_regs = NULL;
  454. break;
  455. }
  456. esp->ops = &mac_esp_ops;
  457. if (mep->pdma_io == NULL) {
  458. printk(KERN_INFO PFX "using PIO for controller %d\n", dev->id);
  459. esp_write8(0, ESP_TCLOW);
  460. esp_write8(0, ESP_TCMED);
  461. esp->flags = ESP_FLAG_DISABLE_SYNC;
  462. mac_esp_ops.send_dma_cmd = mac_esp_send_pio_cmd;
  463. } else {
  464. printk(KERN_INFO PFX "using PDMA for controller %d\n", dev->id);
  465. }
  466. host->irq = IRQ_MAC_SCSI;
  467. esp_chips[dev->id] = esp;
  468. mb();
  469. if (esp_chips[!dev->id] == NULL) {
  470. err = request_irq(host->irq, mac_scsi_esp_intr, 0, "ESP", NULL);
  471. if (err < 0) {
  472. esp_chips[dev->id] = NULL;
  473. goto fail_free_priv;
  474. }
  475. }
  476. err = scsi_esp_register(esp, &dev->dev);
  477. if (err)
  478. goto fail_free_irq;
  479. return 0;
  480. fail_free_irq:
  481. if (esp_chips[!dev->id] == NULL)
  482. free_irq(host->irq, esp);
  483. fail_free_priv:
  484. kfree(mep);
  485. fail_free_command_block:
  486. kfree(esp->command_block);
  487. fail_unlink:
  488. scsi_host_put(host);
  489. fail:
  490. return err;
  491. }
  492. static int esp_mac_remove(struct platform_device *dev)
  493. {
  494. struct mac_esp_priv *mep = platform_get_drvdata(dev);
  495. struct esp *esp = mep->esp;
  496. unsigned int irq = esp->host->irq;
  497. scsi_esp_unregister(esp);
  498. esp_chips[dev->id] = NULL;
  499. if (!(esp_chips[0] || esp_chips[1]))
  500. free_irq(irq, NULL);
  501. kfree(mep);
  502. kfree(esp->command_block);
  503. scsi_host_put(esp->host);
  504. return 0;
  505. }
  506. static struct platform_driver esp_mac_driver = {
  507. .probe = esp_mac_probe,
  508. .remove = esp_mac_remove,
  509. .driver = {
  510. .name = DRV_MODULE_NAME,
  511. },
  512. };
  513. static int __init mac_esp_init(void)
  514. {
  515. return platform_driver_register(&esp_mac_driver);
  516. }
  517. static void __exit mac_esp_exit(void)
  518. {
  519. platform_driver_unregister(&esp_mac_driver);
  520. }
  521. MODULE_DESCRIPTION("Mac ESP SCSI driver");
  522. MODULE_AUTHOR("Finn Thain <fthain@telegraphics.com.au>");
  523. MODULE_LICENSE("GPL v2");
  524. MODULE_VERSION(DRV_VERSION);
  525. MODULE_ALIAS("platform:" DRV_MODULE_NAME);
  526. module_init(mac_esp_init);
  527. module_exit(mac_esp_exit);