mac_esp.c 16 KB

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