mac_esp.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /* mac_esp.c: ESP front-end for Macintosh Quadra systems.
  3. *
  4. * Adapted from jazz_esp.c and the old mac_esp.c.
  5. *
  6. * The pseudo DMA algorithm is based on the one used in NetBSD.
  7. * See sys/arch/mac68k/obio/esp.c for some background information.
  8. *
  9. * Copyright (C) 2007-2008 Finn Thain
  10. */
  11. #include <linux/kernel.h>
  12. #include <linux/types.h>
  13. #include <linux/module.h>
  14. #include <linux/init.h>
  15. #include <linux/interrupt.h>
  16. #include <linux/platform_device.h>
  17. #include <linux/dma-mapping.h>
  18. #include <linux/scatterlist.h>
  19. #include <linux/delay.h>
  20. #include <linux/io.h>
  21. #include <linux/nubus.h>
  22. #include <linux/slab.h>
  23. #include <asm/irq.h>
  24. #include <asm/dma.h>
  25. #include <asm/macints.h>
  26. #include <asm/macintosh.h>
  27. #include <asm/mac_via.h>
  28. #include <scsi/scsi_host.h>
  29. #include "esp_scsi.h"
  30. #define DRV_MODULE_NAME "mac_esp"
  31. #define PFX DRV_MODULE_NAME ": "
  32. #define DRV_VERSION "1.000"
  33. #define DRV_MODULE_RELDATE "Sept 15, 2007"
  34. #define MAC_ESP_IO_BASE 0x50F00000
  35. #define MAC_ESP_REGS_QUADRA (MAC_ESP_IO_BASE + 0x10000)
  36. #define MAC_ESP_REGS_QUADRA2 (MAC_ESP_IO_BASE + 0xF000)
  37. #define MAC_ESP_REGS_QUADRA3 (MAC_ESP_IO_BASE + 0x18000)
  38. #define MAC_ESP_REGS_SPACING 0x402
  39. #define MAC_ESP_PDMA_REG 0xF9800024
  40. #define MAC_ESP_PDMA_REG_SPACING 0x4
  41. #define MAC_ESP_PDMA_IO_OFFSET 0x100
  42. #define esp_read8(REG) mac_esp_read8(esp, REG)
  43. #define esp_write8(VAL, REG) mac_esp_write8(esp, VAL, REG)
  44. struct mac_esp_priv {
  45. struct esp *esp;
  46. void __iomem *pdma_regs;
  47. void __iomem *pdma_io;
  48. };
  49. static struct esp *esp_chips[2];
  50. static DEFINE_SPINLOCK(esp_chips_lock);
  51. #define MAC_ESP_GET_PRIV(esp) ((struct mac_esp_priv *) \
  52. dev_get_drvdata((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. static void mac_esp_reset_dma(struct esp *esp)
  62. {
  63. /* Nothing to do. */
  64. }
  65. static void mac_esp_dma_drain(struct esp *esp)
  66. {
  67. /* Nothing to do. */
  68. }
  69. static void mac_esp_dma_invalidate(struct esp *esp)
  70. {
  71. /* Nothing to do. */
  72. }
  73. static int mac_esp_dma_error(struct esp *esp)
  74. {
  75. return esp->send_cmd_error;
  76. }
  77. static inline int mac_esp_wait_for_empty_fifo(struct esp *esp)
  78. {
  79. int i = 500000;
  80. do {
  81. if (!(esp_read8(ESP_FFLAGS) & ESP_FF_FBYTES))
  82. return 0;
  83. if (esp_read8(ESP_STATUS) & ESP_STAT_INTR)
  84. return 1;
  85. udelay(2);
  86. } while (--i);
  87. printk(KERN_ERR PFX "FIFO is not empty (sreg %02x)\n",
  88. esp_read8(ESP_STATUS));
  89. esp->send_cmd_error = 1;
  90. return 1;
  91. }
  92. static inline int mac_esp_wait_for_dreq(struct esp *esp)
  93. {
  94. struct mac_esp_priv *mep = MAC_ESP_GET_PRIV(esp);
  95. int i = 500000;
  96. do {
  97. if (mep->pdma_regs == NULL) {
  98. if (via2_scsi_drq_pending())
  99. return 0;
  100. } else {
  101. if (nubus_readl(mep->pdma_regs) & 0x200)
  102. return 0;
  103. }
  104. if (esp_read8(ESP_STATUS) & ESP_STAT_INTR)
  105. return 1;
  106. udelay(2);
  107. } while (--i);
  108. printk(KERN_ERR PFX "PDMA timeout (sreg %02x)\n",
  109. esp_read8(ESP_STATUS));
  110. esp->send_cmd_error = 1;
  111. return 1;
  112. }
  113. #define MAC_ESP_PDMA_LOOP(operands) \
  114. asm volatile ( \
  115. " tstw %1 \n" \
  116. " jbeq 20f \n" \
  117. "1: movew " operands " \n" \
  118. "2: movew " operands " \n" \
  119. "3: movew " operands " \n" \
  120. "4: movew " operands " \n" \
  121. "5: movew " operands " \n" \
  122. "6: movew " operands " \n" \
  123. "7: movew " operands " \n" \
  124. "8: movew " operands " \n" \
  125. "9: movew " operands " \n" \
  126. "10: movew " operands " \n" \
  127. "11: movew " operands " \n" \
  128. "12: movew " operands " \n" \
  129. "13: movew " operands " \n" \
  130. "14: movew " operands " \n" \
  131. "15: movew " operands " \n" \
  132. "16: movew " operands " \n" \
  133. " subqw #1,%1 \n" \
  134. " jbne 1b \n" \
  135. "20: tstw %2 \n" \
  136. " jbeq 30f \n" \
  137. "21: movew " operands " \n" \
  138. " subqw #1,%2 \n" \
  139. " jbne 21b \n" \
  140. "30: tstw %3 \n" \
  141. " jbeq 40f \n" \
  142. "31: moveb " operands " \n" \
  143. "32: nop \n" \
  144. "40: \n" \
  145. " \n" \
  146. " .section __ex_table,\"a\" \n" \
  147. " .align 4 \n" \
  148. " .long 1b,40b \n" \
  149. " .long 2b,40b \n" \
  150. " .long 3b,40b \n" \
  151. " .long 4b,40b \n" \
  152. " .long 5b,40b \n" \
  153. " .long 6b,40b \n" \
  154. " .long 7b,40b \n" \
  155. " .long 8b,40b \n" \
  156. " .long 9b,40b \n" \
  157. " .long 10b,40b \n" \
  158. " .long 11b,40b \n" \
  159. " .long 12b,40b \n" \
  160. " .long 13b,40b \n" \
  161. " .long 14b,40b \n" \
  162. " .long 15b,40b \n" \
  163. " .long 16b,40b \n" \
  164. " .long 21b,40b \n" \
  165. " .long 31b,40b \n" \
  166. " .long 32b,40b \n" \
  167. " .previous \n" \
  168. : "+a" (addr), "+r" (count32), "+r" (count2) \
  169. : "g" (count1), "a" (mep->pdma_io))
  170. static void mac_esp_send_pdma_cmd(struct esp *esp, u32 addr, u32 esp_count,
  171. u32 dma_count, int write, u8 cmd)
  172. {
  173. struct mac_esp_priv *mep = MAC_ESP_GET_PRIV(esp);
  174. esp->send_cmd_error = 0;
  175. if (!write)
  176. scsi_esp_cmd(esp, ESP_CMD_FLUSH);
  177. esp_write8((esp_count >> 0) & 0xFF, ESP_TCLOW);
  178. esp_write8((esp_count >> 8) & 0xFF, ESP_TCMED);
  179. scsi_esp_cmd(esp, cmd);
  180. do {
  181. unsigned int count32 = esp_count >> 5;
  182. unsigned int count2 = (esp_count & 0x1F) >> 1;
  183. unsigned int count1 = esp_count & 1;
  184. unsigned int start_addr = addr;
  185. if (mac_esp_wait_for_dreq(esp))
  186. break;
  187. if (write) {
  188. MAC_ESP_PDMA_LOOP("%4@,%0@+");
  189. esp_count -= addr - start_addr;
  190. } else {
  191. unsigned int n;
  192. MAC_ESP_PDMA_LOOP("%0@+,%4@");
  193. if (mac_esp_wait_for_empty_fifo(esp))
  194. break;
  195. n = (esp_read8(ESP_TCMED) << 8) + esp_read8(ESP_TCLOW);
  196. addr = start_addr + esp_count - n;
  197. esp_count = n;
  198. }
  199. } while (esp_count);
  200. }
  201. static int mac_esp_irq_pending(struct esp *esp)
  202. {
  203. if (esp_read8(ESP_STATUS) & ESP_STAT_INTR)
  204. return 1;
  205. return 0;
  206. }
  207. static u32 mac_esp_dma_length_limit(struct esp *esp, u32 dma_addr, u32 dma_len)
  208. {
  209. return dma_len > 0xFFFF ? 0xFFFF : dma_len;
  210. }
  211. static irqreturn_t mac_scsi_esp_intr(int irq, void *dev_id)
  212. {
  213. int got_intr;
  214. /*
  215. * This is an edge triggered IRQ, so we have to be careful to
  216. * avoid missing a transition when it is shared by two ESP devices.
  217. */
  218. do {
  219. got_intr = 0;
  220. if (esp_chips[0] &&
  221. (mac_esp_read8(esp_chips[0], ESP_STATUS) & ESP_STAT_INTR)) {
  222. (void)scsi_esp_intr(irq, esp_chips[0]);
  223. got_intr = 1;
  224. }
  225. if (esp_chips[1] &&
  226. (mac_esp_read8(esp_chips[1], ESP_STATUS) & ESP_STAT_INTR)) {
  227. (void)scsi_esp_intr(irq, esp_chips[1]);
  228. got_intr = 1;
  229. }
  230. } while (got_intr);
  231. return IRQ_HANDLED;
  232. }
  233. static struct esp_driver_ops mac_esp_ops = {
  234. .esp_write8 = mac_esp_write8,
  235. .esp_read8 = mac_esp_read8,
  236. .irq_pending = mac_esp_irq_pending,
  237. .dma_length_limit = mac_esp_dma_length_limit,
  238. .reset_dma = mac_esp_reset_dma,
  239. .dma_drain = mac_esp_dma_drain,
  240. .dma_invalidate = mac_esp_dma_invalidate,
  241. .send_dma_cmd = mac_esp_send_pdma_cmd,
  242. .dma_error = mac_esp_dma_error,
  243. };
  244. static int esp_mac_probe(struct platform_device *dev)
  245. {
  246. struct scsi_host_template *tpnt = &scsi_esp_template;
  247. struct Scsi_Host *host;
  248. struct esp *esp;
  249. int err;
  250. struct mac_esp_priv *mep;
  251. if (!MACH_IS_MAC)
  252. return -ENODEV;
  253. if (dev->id > 1)
  254. return -ENODEV;
  255. host = scsi_host_alloc(tpnt, sizeof(struct esp));
  256. err = -ENOMEM;
  257. if (!host)
  258. goto fail;
  259. host->max_id = 8;
  260. host->dma_boundary = PAGE_SIZE - 1;
  261. esp = shost_priv(host);
  262. esp->host = host;
  263. esp->dev = &dev->dev;
  264. esp->command_block = kzalloc(16, GFP_KERNEL);
  265. if (!esp->command_block)
  266. goto fail_unlink;
  267. esp->command_block_dma = (dma_addr_t)esp->command_block;
  268. esp->scsi_id = 7;
  269. host->this_id = esp->scsi_id;
  270. esp->scsi_id_mask = 1 << esp->scsi_id;
  271. mep = kzalloc(sizeof(struct mac_esp_priv), GFP_KERNEL);
  272. if (!mep)
  273. goto fail_free_command_block;
  274. mep->esp = esp;
  275. platform_set_drvdata(dev, mep);
  276. switch (macintosh_config->scsi_type) {
  277. case MAC_SCSI_QUADRA:
  278. esp->cfreq = 16500000;
  279. esp->regs = (void __iomem *)MAC_ESP_REGS_QUADRA;
  280. mep->pdma_io = esp->regs + MAC_ESP_PDMA_IO_OFFSET;
  281. mep->pdma_regs = NULL;
  282. break;
  283. case MAC_SCSI_QUADRA2:
  284. esp->cfreq = 25000000;
  285. esp->regs = (void __iomem *)(MAC_ESP_REGS_QUADRA2 +
  286. dev->id * MAC_ESP_REGS_SPACING);
  287. mep->pdma_io = esp->regs + MAC_ESP_PDMA_IO_OFFSET;
  288. mep->pdma_regs = (void __iomem *)(MAC_ESP_PDMA_REG +
  289. dev->id * MAC_ESP_PDMA_REG_SPACING);
  290. nubus_writel(0x1d1, mep->pdma_regs);
  291. break;
  292. case MAC_SCSI_QUADRA3:
  293. /* These quadras have a real DMA controller (the PSC) but we
  294. * don't know how to drive it so we must use PIO instead.
  295. */
  296. esp->cfreq = 25000000;
  297. esp->regs = (void __iomem *)MAC_ESP_REGS_QUADRA3;
  298. mep->pdma_io = NULL;
  299. mep->pdma_regs = NULL;
  300. break;
  301. }
  302. esp->fifo_reg = esp->regs + ESP_FDATA * 16;
  303. esp->ops = &mac_esp_ops;
  304. esp->flags = ESP_FLAG_NO_DMA_MAP;
  305. if (mep->pdma_io == NULL) {
  306. printk(KERN_INFO PFX "using PIO for controller %d\n", dev->id);
  307. esp_write8(0, ESP_TCLOW);
  308. esp_write8(0, ESP_TCMED);
  309. esp->flags |= ESP_FLAG_DISABLE_SYNC;
  310. mac_esp_ops.send_dma_cmd = esp_send_pio_cmd;
  311. } else {
  312. printk(KERN_INFO PFX "using PDMA for controller %d\n", dev->id);
  313. }
  314. host->irq = IRQ_MAC_SCSI;
  315. /* The request_irq() call is intended to succeed for the first device
  316. * and fail for the second device.
  317. */
  318. err = request_irq(host->irq, mac_scsi_esp_intr, 0, "ESP", NULL);
  319. spin_lock(&esp_chips_lock);
  320. if (err < 0 && esp_chips[!dev->id] == NULL) {
  321. spin_unlock(&esp_chips_lock);
  322. goto fail_free_priv;
  323. }
  324. esp_chips[dev->id] = esp;
  325. spin_unlock(&esp_chips_lock);
  326. err = scsi_esp_register(esp);
  327. if (err)
  328. goto fail_free_irq;
  329. return 0;
  330. fail_free_irq:
  331. spin_lock(&esp_chips_lock);
  332. esp_chips[dev->id] = NULL;
  333. if (esp_chips[!dev->id] == NULL) {
  334. spin_unlock(&esp_chips_lock);
  335. free_irq(host->irq, NULL);
  336. } else
  337. spin_unlock(&esp_chips_lock);
  338. fail_free_priv:
  339. kfree(mep);
  340. fail_free_command_block:
  341. kfree(esp->command_block);
  342. fail_unlink:
  343. scsi_host_put(host);
  344. fail:
  345. return err;
  346. }
  347. static int esp_mac_remove(struct platform_device *dev)
  348. {
  349. struct mac_esp_priv *mep = platform_get_drvdata(dev);
  350. struct esp *esp = mep->esp;
  351. unsigned int irq = esp->host->irq;
  352. scsi_esp_unregister(esp);
  353. spin_lock(&esp_chips_lock);
  354. esp_chips[dev->id] = NULL;
  355. if (esp_chips[!dev->id] == NULL) {
  356. spin_unlock(&esp_chips_lock);
  357. free_irq(irq, NULL);
  358. } else
  359. spin_unlock(&esp_chips_lock);
  360. kfree(mep);
  361. kfree(esp->command_block);
  362. scsi_host_put(esp->host);
  363. return 0;
  364. }
  365. static struct platform_driver esp_mac_driver = {
  366. .probe = esp_mac_probe,
  367. .remove = esp_mac_remove,
  368. .driver = {
  369. .name = DRV_MODULE_NAME,
  370. },
  371. };
  372. module_platform_driver(esp_mac_driver);
  373. MODULE_DESCRIPTION("Mac ESP SCSI driver");
  374. MODULE_AUTHOR("Finn Thain");
  375. MODULE_LICENSE("GPL v2");
  376. MODULE_VERSION(DRV_VERSION);
  377. MODULE_ALIAS("platform:" DRV_MODULE_NAME);