zorro_esp.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * ESP front-end for Amiga ZORRO SCSI systems.
  4. *
  5. * Copyright (C) 1996 Jesper Skov (jskov@cygnus.co.uk)
  6. *
  7. * Copyright (C) 2011,2018 Michael Schmitz (schmitz@debian.org) for
  8. * migration to ESP SCSI core
  9. *
  10. * Copyright (C) 2013 Tuomas Vainikka (tuomas.vainikka@aalto.fi) for
  11. * Blizzard 1230 DMA and probe function fixes
  12. *
  13. * Copyright (C) 2017 Finn Thain for PIO code from Mac ESP driver adapted here
  14. */
  15. /*
  16. * ZORRO bus code from:
  17. */
  18. /*
  19. * Detection routine for the NCR53c710 based Amiga SCSI Controllers for Linux.
  20. * Amiga MacroSystemUS WarpEngine SCSI controller.
  21. * Amiga Technologies/DKB A4091 SCSI controller.
  22. *
  23. * Written 1997 by Alan Hourihane <alanh@fairlite.demon.co.uk>
  24. * plus modifications of the 53c7xx.c driver to support the Amiga.
  25. *
  26. * Rewritten to use 53c700.c by Kars de Jong <jongk@linux-m68k.org>
  27. */
  28. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  29. #include <linux/module.h>
  30. #include <linux/init.h>
  31. #include <linux/interrupt.h>
  32. #include <linux/dma-mapping.h>
  33. #include <linux/scatterlist.h>
  34. #include <linux/delay.h>
  35. #include <linux/zorro.h>
  36. #include <linux/slab.h>
  37. #include <asm/page.h>
  38. #include <asm/pgtable.h>
  39. #include <asm/cacheflush.h>
  40. #include <asm/amigahw.h>
  41. #include <asm/amigaints.h>
  42. #include <scsi/scsi_host.h>
  43. #include <scsi/scsi_transport_spi.h>
  44. #include <scsi/scsi_device.h>
  45. #include <scsi/scsi_tcq.h>
  46. #include "esp_scsi.h"
  47. MODULE_AUTHOR("Michael Schmitz <schmitz@debian.org>");
  48. MODULE_DESCRIPTION("Amiga Zorro NCR5C9x (ESP) driver");
  49. MODULE_LICENSE("GPL");
  50. /* per-board register layout definitions */
  51. /* Blizzard 1230 DMA interface */
  52. struct blz1230_dma_registers {
  53. unsigned char dma_addr; /* DMA address [0x0000] */
  54. unsigned char dmapad2[0x7fff];
  55. unsigned char dma_latch; /* DMA latch [0x8000] */
  56. };
  57. /* Blizzard 1230II DMA interface */
  58. struct blz1230II_dma_registers {
  59. unsigned char dma_addr; /* DMA address [0x0000] */
  60. unsigned char dmapad2[0xf];
  61. unsigned char dma_latch; /* DMA latch [0x0010] */
  62. };
  63. /* Blizzard 2060 DMA interface */
  64. struct blz2060_dma_registers {
  65. unsigned char dma_led_ctrl; /* DMA led control [0x000] */
  66. unsigned char dmapad1[0x0f];
  67. unsigned char dma_addr0; /* DMA address (MSB) [0x010] */
  68. unsigned char dmapad2[0x03];
  69. unsigned char dma_addr1; /* DMA address [0x014] */
  70. unsigned char dmapad3[0x03];
  71. unsigned char dma_addr2; /* DMA address [0x018] */
  72. unsigned char dmapad4[0x03];
  73. unsigned char dma_addr3; /* DMA address (LSB) [0x01c] */
  74. };
  75. /* DMA control bits */
  76. #define DMA_WRITE 0x80000000
  77. /* Cyberstorm DMA interface */
  78. struct cyber_dma_registers {
  79. unsigned char dma_addr0; /* DMA address (MSB) [0x000] */
  80. unsigned char dmapad1[1];
  81. unsigned char dma_addr1; /* DMA address [0x002] */
  82. unsigned char dmapad2[1];
  83. unsigned char dma_addr2; /* DMA address [0x004] */
  84. unsigned char dmapad3[1];
  85. unsigned char dma_addr3; /* DMA address (LSB) [0x006] */
  86. unsigned char dmapad4[0x3fb];
  87. unsigned char cond_reg; /* DMA cond (ro) [0x402] */
  88. #define ctrl_reg cond_reg /* DMA control (wo) [0x402] */
  89. };
  90. /* DMA control bits */
  91. #define CYBER_DMA_WRITE 0x40 /* DMA direction. 1 = write */
  92. #define CYBER_DMA_Z3 0x20 /* 16 (Z2) or 32 (CHIP/Z3) bit DMA transfer */
  93. /* DMA status bits */
  94. #define CYBER_DMA_HNDL_INTR 0x80 /* DMA IRQ pending? */
  95. /* The CyberStorm II DMA interface */
  96. struct cyberII_dma_registers {
  97. unsigned char cond_reg; /* DMA cond (ro) [0x000] */
  98. #define ctrl_reg cond_reg /* DMA control (wo) [0x000] */
  99. unsigned char dmapad4[0x3f];
  100. unsigned char dma_addr0; /* DMA address (MSB) [0x040] */
  101. unsigned char dmapad1[3];
  102. unsigned char dma_addr1; /* DMA address [0x044] */
  103. unsigned char dmapad2[3];
  104. unsigned char dma_addr2; /* DMA address [0x048] */
  105. unsigned char dmapad3[3];
  106. unsigned char dma_addr3; /* DMA address (LSB) [0x04c] */
  107. };
  108. /* Fastlane DMA interface */
  109. struct fastlane_dma_registers {
  110. unsigned char cond_reg; /* DMA status (ro) [0x0000] */
  111. #define ctrl_reg cond_reg /* DMA control (wo) [0x0000] */
  112. char dmapad1[0x3f];
  113. unsigned char clear_strobe; /* DMA clear (wo) [0x0040] */
  114. };
  115. /*
  116. * The controller registers can be found in the Z2 config area at these
  117. * offsets:
  118. */
  119. #define FASTLANE_ESP_ADDR 0x1000001
  120. /* DMA status bits */
  121. #define FASTLANE_DMA_MINT 0x80
  122. #define FASTLANE_DMA_IACT 0x40
  123. #define FASTLANE_DMA_CREQ 0x20
  124. /* DMA control bits */
  125. #define FASTLANE_DMA_FCODE 0xa0
  126. #define FASTLANE_DMA_MASK 0xf3
  127. #define FASTLANE_DMA_WRITE 0x08 /* 1 = write */
  128. #define FASTLANE_DMA_ENABLE 0x04 /* Enable DMA */
  129. #define FASTLANE_DMA_EDI 0x02 /* Enable DMA IRQ ? */
  130. #define FASTLANE_DMA_ESI 0x01 /* Enable SCSI IRQ */
  131. /*
  132. * private data used for driver
  133. */
  134. struct zorro_esp_priv {
  135. struct esp *esp; /* our ESP instance - for Scsi_host* */
  136. void __iomem *board_base; /* virtual address (Zorro III board) */
  137. int error; /* PIO error flag */
  138. int zorro3; /* board is Zorro III */
  139. unsigned char ctrl_data; /* shadow copy of ctrl_reg */
  140. };
  141. /*
  142. * On all implementations except for the Oktagon, padding between ESP
  143. * registers is three bytes.
  144. * On Oktagon, it is one byte - use a different accessor there.
  145. *
  146. * Oktagon needs PDMA - currently unsupported!
  147. */
  148. static void zorro_esp_write8(struct esp *esp, u8 val, unsigned long reg)
  149. {
  150. writeb(val, esp->regs + (reg * 4UL));
  151. }
  152. static u8 zorro_esp_read8(struct esp *esp, unsigned long reg)
  153. {
  154. return readb(esp->regs + (reg * 4UL));
  155. }
  156. static dma_addr_t zorro_esp_map_single(struct esp *esp, void *buf,
  157. size_t sz, int dir)
  158. {
  159. return dma_map_single(esp->dev, buf, sz, dir);
  160. }
  161. static int zorro_esp_map_sg(struct esp *esp, struct scatterlist *sg,
  162. int num_sg, int dir)
  163. {
  164. return dma_map_sg(esp->dev, sg, num_sg, dir);
  165. }
  166. static void zorro_esp_unmap_single(struct esp *esp, dma_addr_t addr,
  167. size_t sz, int dir)
  168. {
  169. dma_unmap_single(esp->dev, addr, sz, dir);
  170. }
  171. static void zorro_esp_unmap_sg(struct esp *esp, struct scatterlist *sg,
  172. int num_sg, int dir)
  173. {
  174. dma_unmap_sg(esp->dev, sg, num_sg, dir);
  175. }
  176. static int zorro_esp_irq_pending(struct esp *esp)
  177. {
  178. /* check ESP status register; DMA has no status reg. */
  179. if (zorro_esp_read8(esp, ESP_STATUS) & ESP_STAT_INTR)
  180. return 1;
  181. return 0;
  182. }
  183. static int cyber_esp_irq_pending(struct esp *esp)
  184. {
  185. struct cyber_dma_registers __iomem *dregs = esp->dma_regs;
  186. unsigned char dma_status = readb(&dregs->cond_reg);
  187. /* It's important to check the DMA IRQ bit in the correct way! */
  188. return ((zorro_esp_read8(esp, ESP_STATUS) & ESP_STAT_INTR) &&
  189. (dma_status & CYBER_DMA_HNDL_INTR));
  190. }
  191. static int fastlane_esp_irq_pending(struct esp *esp)
  192. {
  193. struct fastlane_dma_registers __iomem *dregs = esp->dma_regs;
  194. unsigned char dma_status;
  195. dma_status = readb(&dregs->cond_reg);
  196. if (dma_status & FASTLANE_DMA_IACT)
  197. return 0; /* not our IRQ */
  198. /* Return non-zero if ESP requested IRQ */
  199. return (
  200. (dma_status & FASTLANE_DMA_CREQ) &&
  201. (!(dma_status & FASTLANE_DMA_MINT)) &&
  202. (zorro_esp_read8(esp, ESP_STATUS) & ESP_STAT_INTR));
  203. }
  204. static u32 zorro_esp_dma_length_limit(struct esp *esp, u32 dma_addr,
  205. u32 dma_len)
  206. {
  207. return dma_len > (1U << 16) ? (1U << 16) : dma_len;
  208. }
  209. static u32 fastlane_esp_dma_length_limit(struct esp *esp, u32 dma_addr,
  210. u32 dma_len)
  211. {
  212. /* The old driver used 0xfffc as limit, so do that here too */
  213. return dma_len > 0xfffc ? 0xfffc : dma_len;
  214. }
  215. static void zorro_esp_reset_dma(struct esp *esp)
  216. {
  217. /* nothing to do here */
  218. }
  219. static void zorro_esp_dma_drain(struct esp *esp)
  220. {
  221. /* nothing to do here */
  222. }
  223. static void zorro_esp_dma_invalidate(struct esp *esp)
  224. {
  225. /* nothing to do here */
  226. }
  227. static void fastlane_esp_dma_invalidate(struct esp *esp)
  228. {
  229. struct zorro_esp_priv *zep = dev_get_drvdata(esp->dev);
  230. struct fastlane_dma_registers __iomem *dregs = esp->dma_regs;
  231. unsigned char *ctrl_data = &zep->ctrl_data;
  232. *ctrl_data = (*ctrl_data & FASTLANE_DMA_MASK);
  233. writeb(0, &dregs->clear_strobe);
  234. z_writel(0, zep->board_base);
  235. }
  236. /*
  237. * Programmed IO routines follow.
  238. */
  239. static inline unsigned int zorro_esp_wait_for_fifo(struct esp *esp)
  240. {
  241. int i = 500000;
  242. do {
  243. unsigned int fbytes = zorro_esp_read8(esp, ESP_FFLAGS)
  244. & ESP_FF_FBYTES;
  245. if (fbytes)
  246. return fbytes;
  247. udelay(2);
  248. } while (--i);
  249. pr_err("FIFO is empty (sreg %02x)\n",
  250. zorro_esp_read8(esp, ESP_STATUS));
  251. return 0;
  252. }
  253. static inline int zorro_esp_wait_for_intr(struct esp *esp)
  254. {
  255. struct zorro_esp_priv *zep = dev_get_drvdata(esp->dev);
  256. int i = 500000;
  257. do {
  258. esp->sreg = zorro_esp_read8(esp, ESP_STATUS);
  259. if (esp->sreg & ESP_STAT_INTR)
  260. return 0;
  261. udelay(2);
  262. } while (--i);
  263. pr_err("IRQ timeout (sreg %02x)\n", esp->sreg);
  264. zep->error = 1;
  265. return 1;
  266. }
  267. /*
  268. * PIO macros as used in mac_esp.c.
  269. * Note that addr and fifo arguments are local-scope variables declared
  270. * in zorro_esp_send_pio_cmd(), the macros are only used in that function,
  271. * and addr and fifo are referenced in each use of the macros so there
  272. * is no need to pass them as macro parameters.
  273. */
  274. #define ZORRO_ESP_PIO_LOOP(operands, reg1) \
  275. asm volatile ( \
  276. "1: moveb " operands "\n" \
  277. " subqw #1,%1 \n" \
  278. " jbne 1b \n" \
  279. : "+a" (addr), "+r" (reg1) \
  280. : "a" (fifo));
  281. #define ZORRO_ESP_PIO_FILL(operands, reg1) \
  282. asm volatile ( \
  283. " moveb " operands "\n" \
  284. " moveb " operands "\n" \
  285. " moveb " operands "\n" \
  286. " moveb " operands "\n" \
  287. " moveb " operands "\n" \
  288. " moveb " operands "\n" \
  289. " moveb " operands "\n" \
  290. " moveb " operands "\n" \
  291. " moveb " operands "\n" \
  292. " moveb " operands "\n" \
  293. " moveb " operands "\n" \
  294. " moveb " operands "\n" \
  295. " moveb " operands "\n" \
  296. " moveb " operands "\n" \
  297. " moveb " operands "\n" \
  298. " moveb " operands "\n" \
  299. " subqw #8,%1 \n" \
  300. " subqw #8,%1 \n" \
  301. : "+a" (addr), "+r" (reg1) \
  302. : "a" (fifo));
  303. #define ZORRO_ESP_FIFO_SIZE 16
  304. static void zorro_esp_send_pio_cmd(struct esp *esp, u32 addr, u32 esp_count,
  305. u32 dma_count, int write, u8 cmd)
  306. {
  307. struct zorro_esp_priv *zep = dev_get_drvdata(esp->dev);
  308. u8 __iomem *fifo = esp->regs + ESP_FDATA * 16;
  309. u8 phase = esp->sreg & ESP_STAT_PMASK;
  310. cmd &= ~ESP_CMD_DMA;
  311. if (write) {
  312. u8 *dst = (u8 *)addr;
  313. u8 mask = ~(phase == ESP_MIP ? ESP_INTR_FDONE : ESP_INTR_BSERV);
  314. scsi_esp_cmd(esp, cmd);
  315. while (1) {
  316. if (!zorro_esp_wait_for_fifo(esp))
  317. break;
  318. *dst++ = zorro_esp_read8(esp, ESP_FDATA);
  319. --esp_count;
  320. if (!esp_count)
  321. break;
  322. if (zorro_esp_wait_for_intr(esp))
  323. break;
  324. if ((esp->sreg & ESP_STAT_PMASK) != phase)
  325. break;
  326. esp->ireg = zorro_esp_read8(esp, ESP_INTRPT);
  327. if (esp->ireg & mask) {
  328. zep->error = 1;
  329. break;
  330. }
  331. if (phase == ESP_MIP)
  332. scsi_esp_cmd(esp, ESP_CMD_MOK);
  333. scsi_esp_cmd(esp, ESP_CMD_TI);
  334. }
  335. } else { /* unused, as long as we only handle MIP here */
  336. scsi_esp_cmd(esp, ESP_CMD_FLUSH);
  337. if (esp_count >= ZORRO_ESP_FIFO_SIZE)
  338. ZORRO_ESP_PIO_FILL("%0@+,%2@", esp_count)
  339. else
  340. ZORRO_ESP_PIO_LOOP("%0@+,%2@", esp_count)
  341. scsi_esp_cmd(esp, cmd);
  342. while (esp_count) {
  343. unsigned int n;
  344. if (zorro_esp_wait_for_intr(esp))
  345. break;
  346. if ((esp->sreg & ESP_STAT_PMASK) != phase)
  347. break;
  348. esp->ireg = zorro_esp_read8(esp, ESP_INTRPT);
  349. if (esp->ireg & ~ESP_INTR_BSERV) {
  350. zep->error = 1;
  351. break;
  352. }
  353. n = ZORRO_ESP_FIFO_SIZE -
  354. (zorro_esp_read8(esp, ESP_FFLAGS) & ESP_FF_FBYTES);
  355. if (n > esp_count)
  356. n = esp_count;
  357. if (n == ZORRO_ESP_FIFO_SIZE)
  358. ZORRO_ESP_PIO_FILL("%0@+,%2@", esp_count)
  359. else {
  360. esp_count -= n;
  361. ZORRO_ESP_PIO_LOOP("%0@+,%2@", n)
  362. }
  363. scsi_esp_cmd(esp, ESP_CMD_TI);
  364. }
  365. }
  366. }
  367. /* Blizzard 1230/60 SCSI-IV DMA */
  368. static void zorro_esp_send_blz1230_dma_cmd(struct esp *esp, u32 addr,
  369. u32 esp_count, u32 dma_count, int write, u8 cmd)
  370. {
  371. struct zorro_esp_priv *zep = dev_get_drvdata(esp->dev);
  372. struct blz1230_dma_registers __iomem *dregs = esp->dma_regs;
  373. u8 phase = esp->sreg & ESP_STAT_PMASK;
  374. zep->error = 0;
  375. /*
  376. * Use PIO if transferring message bytes to esp->command_block_dma.
  377. * PIO requires a virtual address, so substitute esp->command_block
  378. * for addr.
  379. */
  380. if (phase == ESP_MIP && addr == esp->command_block_dma) {
  381. zorro_esp_send_pio_cmd(esp, (u32) esp->command_block,
  382. esp_count, dma_count, write, cmd);
  383. return;
  384. }
  385. if (write)
  386. /* DMA receive */
  387. dma_sync_single_for_device(esp->dev, addr, esp_count,
  388. DMA_FROM_DEVICE);
  389. else
  390. /* DMA send */
  391. dma_sync_single_for_device(esp->dev, addr, esp_count,
  392. DMA_TO_DEVICE);
  393. addr >>= 1;
  394. if (write)
  395. addr &= ~(DMA_WRITE);
  396. else
  397. addr |= DMA_WRITE;
  398. writeb((addr >> 24) & 0xff, &dregs->dma_latch);
  399. writeb((addr >> 24) & 0xff, &dregs->dma_addr);
  400. writeb((addr >> 16) & 0xff, &dregs->dma_addr);
  401. writeb((addr >> 8) & 0xff, &dregs->dma_addr);
  402. writeb(addr & 0xff, &dregs->dma_addr);
  403. scsi_esp_cmd(esp, ESP_CMD_DMA);
  404. zorro_esp_write8(esp, (esp_count >> 0) & 0xff, ESP_TCLOW);
  405. zorro_esp_write8(esp, (esp_count >> 8) & 0xff, ESP_TCMED);
  406. scsi_esp_cmd(esp, cmd);
  407. }
  408. /* Blizzard 1230-II DMA */
  409. static void zorro_esp_send_blz1230II_dma_cmd(struct esp *esp, u32 addr,
  410. u32 esp_count, u32 dma_count, int write, u8 cmd)
  411. {
  412. struct zorro_esp_priv *zep = dev_get_drvdata(esp->dev);
  413. struct blz1230II_dma_registers __iomem *dregs = esp->dma_regs;
  414. u8 phase = esp->sreg & ESP_STAT_PMASK;
  415. zep->error = 0;
  416. /* Use PIO if transferring message bytes to esp->command_block_dma */
  417. if (phase == ESP_MIP && addr == esp->command_block_dma) {
  418. zorro_esp_send_pio_cmd(esp, (u32) esp->command_block,
  419. esp_count, dma_count, write, cmd);
  420. return;
  421. }
  422. if (write)
  423. /* DMA receive */
  424. dma_sync_single_for_device(esp->dev, addr, esp_count,
  425. DMA_FROM_DEVICE);
  426. else
  427. /* DMA send */
  428. dma_sync_single_for_device(esp->dev, addr, esp_count,
  429. DMA_TO_DEVICE);
  430. addr >>= 1;
  431. if (write)
  432. addr &= ~(DMA_WRITE);
  433. else
  434. addr |= DMA_WRITE;
  435. writeb((addr >> 24) & 0xff, &dregs->dma_latch);
  436. writeb((addr >> 16) & 0xff, &dregs->dma_addr);
  437. writeb((addr >> 8) & 0xff, &dregs->dma_addr);
  438. writeb(addr & 0xff, &dregs->dma_addr);
  439. scsi_esp_cmd(esp, ESP_CMD_DMA);
  440. zorro_esp_write8(esp, (esp_count >> 0) & 0xff, ESP_TCLOW);
  441. zorro_esp_write8(esp, (esp_count >> 8) & 0xff, ESP_TCMED);
  442. scsi_esp_cmd(esp, cmd);
  443. }
  444. /* Blizzard 2060 DMA */
  445. static void zorro_esp_send_blz2060_dma_cmd(struct esp *esp, u32 addr,
  446. u32 esp_count, u32 dma_count, int write, u8 cmd)
  447. {
  448. struct zorro_esp_priv *zep = dev_get_drvdata(esp->dev);
  449. struct blz2060_dma_registers __iomem *dregs = esp->dma_regs;
  450. u8 phase = esp->sreg & ESP_STAT_PMASK;
  451. zep->error = 0;
  452. /* Use PIO if transferring message bytes to esp->command_block_dma */
  453. if (phase == ESP_MIP && addr == esp->command_block_dma) {
  454. zorro_esp_send_pio_cmd(esp, (u32) esp->command_block,
  455. esp_count, dma_count, write, cmd);
  456. return;
  457. }
  458. if (write)
  459. /* DMA receive */
  460. dma_sync_single_for_device(esp->dev, addr, esp_count,
  461. DMA_FROM_DEVICE);
  462. else
  463. /* DMA send */
  464. dma_sync_single_for_device(esp->dev, addr, esp_count,
  465. DMA_TO_DEVICE);
  466. addr >>= 1;
  467. if (write)
  468. addr &= ~(DMA_WRITE);
  469. else
  470. addr |= DMA_WRITE;
  471. writeb(addr & 0xff, &dregs->dma_addr3);
  472. writeb((addr >> 8) & 0xff, &dregs->dma_addr2);
  473. writeb((addr >> 16) & 0xff, &dregs->dma_addr1);
  474. writeb((addr >> 24) & 0xff, &dregs->dma_addr0);
  475. scsi_esp_cmd(esp, ESP_CMD_DMA);
  476. zorro_esp_write8(esp, (esp_count >> 0) & 0xff, ESP_TCLOW);
  477. zorro_esp_write8(esp, (esp_count >> 8) & 0xff, ESP_TCMED);
  478. scsi_esp_cmd(esp, cmd);
  479. }
  480. /* Cyberstorm I DMA */
  481. static void zorro_esp_send_cyber_dma_cmd(struct esp *esp, u32 addr,
  482. u32 esp_count, u32 dma_count, int write, u8 cmd)
  483. {
  484. struct zorro_esp_priv *zep = dev_get_drvdata(esp->dev);
  485. struct cyber_dma_registers __iomem *dregs = esp->dma_regs;
  486. u8 phase = esp->sreg & ESP_STAT_PMASK;
  487. unsigned char *ctrl_data = &zep->ctrl_data;
  488. zep->error = 0;
  489. /* Use PIO if transferring message bytes to esp->command_block_dma */
  490. if (phase == ESP_MIP && addr == esp->command_block_dma) {
  491. zorro_esp_send_pio_cmd(esp, (u32) esp->command_block,
  492. esp_count, dma_count, write, cmd);
  493. return;
  494. }
  495. zorro_esp_write8(esp, (esp_count >> 0) & 0xff, ESP_TCLOW);
  496. zorro_esp_write8(esp, (esp_count >> 8) & 0xff, ESP_TCMED);
  497. if (write) {
  498. /* DMA receive */
  499. dma_sync_single_for_device(esp->dev, addr, esp_count,
  500. DMA_FROM_DEVICE);
  501. addr &= ~(1);
  502. } else {
  503. /* DMA send */
  504. dma_sync_single_for_device(esp->dev, addr, esp_count,
  505. DMA_TO_DEVICE);
  506. addr |= 1;
  507. }
  508. writeb((addr >> 24) & 0xff, &dregs->dma_addr0);
  509. writeb((addr >> 16) & 0xff, &dregs->dma_addr1);
  510. writeb((addr >> 8) & 0xff, &dregs->dma_addr2);
  511. writeb(addr & 0xff, &dregs->dma_addr3);
  512. if (write)
  513. *ctrl_data &= ~(CYBER_DMA_WRITE);
  514. else
  515. *ctrl_data |= CYBER_DMA_WRITE;
  516. *ctrl_data &= ~(CYBER_DMA_Z3); /* Z2, do 16 bit DMA */
  517. writeb(*ctrl_data, &dregs->ctrl_reg);
  518. scsi_esp_cmd(esp, cmd);
  519. }
  520. /* Cyberstorm II DMA */
  521. static void zorro_esp_send_cyberII_dma_cmd(struct esp *esp, u32 addr,
  522. u32 esp_count, u32 dma_count, int write, u8 cmd)
  523. {
  524. struct zorro_esp_priv *zep = dev_get_drvdata(esp->dev);
  525. struct cyberII_dma_registers __iomem *dregs = esp->dma_regs;
  526. u8 phase = esp->sreg & ESP_STAT_PMASK;
  527. zep->error = 0;
  528. /* Use PIO if transferring message bytes to esp->command_block_dma */
  529. if (phase == ESP_MIP && addr == esp->command_block_dma) {
  530. zorro_esp_send_pio_cmd(esp, (u32) esp->command_block,
  531. esp_count, dma_count, write, cmd);
  532. return;
  533. }
  534. zorro_esp_write8(esp, (esp_count >> 0) & 0xff, ESP_TCLOW);
  535. zorro_esp_write8(esp, (esp_count >> 8) & 0xff, ESP_TCMED);
  536. if (write) {
  537. /* DMA receive */
  538. dma_sync_single_for_device(esp->dev, addr, esp_count,
  539. DMA_FROM_DEVICE);
  540. addr &= ~(1);
  541. } else {
  542. /* DMA send */
  543. dma_sync_single_for_device(esp->dev, addr, esp_count,
  544. DMA_TO_DEVICE);
  545. addr |= 1;
  546. }
  547. writeb((addr >> 24) & 0xff, &dregs->dma_addr0);
  548. writeb((addr >> 16) & 0xff, &dregs->dma_addr1);
  549. writeb((addr >> 8) & 0xff, &dregs->dma_addr2);
  550. writeb(addr & 0xff, &dregs->dma_addr3);
  551. scsi_esp_cmd(esp, cmd);
  552. }
  553. /* Fastlane DMA */
  554. static void zorro_esp_send_fastlane_dma_cmd(struct esp *esp, u32 addr,
  555. u32 esp_count, u32 dma_count, int write, u8 cmd)
  556. {
  557. struct zorro_esp_priv *zep = dev_get_drvdata(esp->dev);
  558. struct fastlane_dma_registers __iomem *dregs = esp->dma_regs;
  559. u8 phase = esp->sreg & ESP_STAT_PMASK;
  560. unsigned char *ctrl_data = &zep->ctrl_data;
  561. zep->error = 0;
  562. /* Use PIO if transferring message bytes to esp->command_block_dma */
  563. if (phase == ESP_MIP && addr == esp->command_block_dma) {
  564. zorro_esp_send_pio_cmd(esp, (u32) esp->command_block,
  565. esp_count, dma_count, write, cmd);
  566. return;
  567. }
  568. zorro_esp_write8(esp, (esp_count >> 0) & 0xff, ESP_TCLOW);
  569. zorro_esp_write8(esp, (esp_count >> 8) & 0xff, ESP_TCMED);
  570. if (write) {
  571. /* DMA receive */
  572. dma_sync_single_for_device(esp->dev, addr, esp_count,
  573. DMA_FROM_DEVICE);
  574. addr &= ~(1);
  575. } else {
  576. /* DMA send */
  577. dma_sync_single_for_device(esp->dev, addr, esp_count,
  578. DMA_TO_DEVICE);
  579. addr |= 1;
  580. }
  581. writeb(0, &dregs->clear_strobe);
  582. z_writel(addr, ((addr & 0x00ffffff) + zep->board_base));
  583. if (write) {
  584. *ctrl_data = (*ctrl_data & FASTLANE_DMA_MASK) |
  585. FASTLANE_DMA_ENABLE;
  586. } else {
  587. *ctrl_data = ((*ctrl_data & FASTLANE_DMA_MASK) |
  588. FASTLANE_DMA_ENABLE |
  589. FASTLANE_DMA_WRITE);
  590. }
  591. writeb(*ctrl_data, &dregs->ctrl_reg);
  592. scsi_esp_cmd(esp, cmd);
  593. }
  594. static int zorro_esp_dma_error(struct esp *esp)
  595. {
  596. struct zorro_esp_priv *zep = dev_get_drvdata(esp->dev);
  597. /* check for error in case we've been doing PIO */
  598. if (zep->error == 1)
  599. return 1;
  600. /* do nothing - there seems to be no way to check for DMA errors */
  601. return 0;
  602. }
  603. /* per-board ESP driver ops */
  604. static const struct esp_driver_ops blz1230_esp_ops = {
  605. .esp_write8 = zorro_esp_write8,
  606. .esp_read8 = zorro_esp_read8,
  607. .map_single = zorro_esp_map_single,
  608. .map_sg = zorro_esp_map_sg,
  609. .unmap_single = zorro_esp_unmap_single,
  610. .unmap_sg = zorro_esp_unmap_sg,
  611. .irq_pending = zorro_esp_irq_pending,
  612. .dma_length_limit = zorro_esp_dma_length_limit,
  613. .reset_dma = zorro_esp_reset_dma,
  614. .dma_drain = zorro_esp_dma_drain,
  615. .dma_invalidate = zorro_esp_dma_invalidate,
  616. .send_dma_cmd = zorro_esp_send_blz1230_dma_cmd,
  617. .dma_error = zorro_esp_dma_error,
  618. };
  619. static const struct esp_driver_ops blz1230II_esp_ops = {
  620. .esp_write8 = zorro_esp_write8,
  621. .esp_read8 = zorro_esp_read8,
  622. .map_single = zorro_esp_map_single,
  623. .map_sg = zorro_esp_map_sg,
  624. .unmap_single = zorro_esp_unmap_single,
  625. .unmap_sg = zorro_esp_unmap_sg,
  626. .irq_pending = zorro_esp_irq_pending,
  627. .dma_length_limit = zorro_esp_dma_length_limit,
  628. .reset_dma = zorro_esp_reset_dma,
  629. .dma_drain = zorro_esp_dma_drain,
  630. .dma_invalidate = zorro_esp_dma_invalidate,
  631. .send_dma_cmd = zorro_esp_send_blz1230II_dma_cmd,
  632. .dma_error = zorro_esp_dma_error,
  633. };
  634. static const struct esp_driver_ops blz2060_esp_ops = {
  635. .esp_write8 = zorro_esp_write8,
  636. .esp_read8 = zorro_esp_read8,
  637. .map_single = zorro_esp_map_single,
  638. .map_sg = zorro_esp_map_sg,
  639. .unmap_single = zorro_esp_unmap_single,
  640. .unmap_sg = zorro_esp_unmap_sg,
  641. .irq_pending = zorro_esp_irq_pending,
  642. .dma_length_limit = zorro_esp_dma_length_limit,
  643. .reset_dma = zorro_esp_reset_dma,
  644. .dma_drain = zorro_esp_dma_drain,
  645. .dma_invalidate = zorro_esp_dma_invalidate,
  646. .send_dma_cmd = zorro_esp_send_blz2060_dma_cmd,
  647. .dma_error = zorro_esp_dma_error,
  648. };
  649. static const struct esp_driver_ops cyber_esp_ops = {
  650. .esp_write8 = zorro_esp_write8,
  651. .esp_read8 = zorro_esp_read8,
  652. .map_single = zorro_esp_map_single,
  653. .map_sg = zorro_esp_map_sg,
  654. .unmap_single = zorro_esp_unmap_single,
  655. .unmap_sg = zorro_esp_unmap_sg,
  656. .irq_pending = cyber_esp_irq_pending,
  657. .dma_length_limit = zorro_esp_dma_length_limit,
  658. .reset_dma = zorro_esp_reset_dma,
  659. .dma_drain = zorro_esp_dma_drain,
  660. .dma_invalidate = zorro_esp_dma_invalidate,
  661. .send_dma_cmd = zorro_esp_send_cyber_dma_cmd,
  662. .dma_error = zorro_esp_dma_error,
  663. };
  664. static const struct esp_driver_ops cyberII_esp_ops = {
  665. .esp_write8 = zorro_esp_write8,
  666. .esp_read8 = zorro_esp_read8,
  667. .map_single = zorro_esp_map_single,
  668. .map_sg = zorro_esp_map_sg,
  669. .unmap_single = zorro_esp_unmap_single,
  670. .unmap_sg = zorro_esp_unmap_sg,
  671. .irq_pending = zorro_esp_irq_pending,
  672. .dma_length_limit = zorro_esp_dma_length_limit,
  673. .reset_dma = zorro_esp_reset_dma,
  674. .dma_drain = zorro_esp_dma_drain,
  675. .dma_invalidate = zorro_esp_dma_invalidate,
  676. .send_dma_cmd = zorro_esp_send_cyberII_dma_cmd,
  677. .dma_error = zorro_esp_dma_error,
  678. };
  679. static const struct esp_driver_ops fastlane_esp_ops = {
  680. .esp_write8 = zorro_esp_write8,
  681. .esp_read8 = zorro_esp_read8,
  682. .map_single = zorro_esp_map_single,
  683. .map_sg = zorro_esp_map_sg,
  684. .unmap_single = zorro_esp_unmap_single,
  685. .unmap_sg = zorro_esp_unmap_sg,
  686. .irq_pending = fastlane_esp_irq_pending,
  687. .dma_length_limit = fastlane_esp_dma_length_limit,
  688. .reset_dma = zorro_esp_reset_dma,
  689. .dma_drain = zorro_esp_dma_drain,
  690. .dma_invalidate = fastlane_esp_dma_invalidate,
  691. .send_dma_cmd = zorro_esp_send_fastlane_dma_cmd,
  692. .dma_error = zorro_esp_dma_error,
  693. };
  694. /* Zorro driver config data */
  695. struct zorro_driver_data {
  696. const char *name;
  697. unsigned long offset;
  698. unsigned long dma_offset;
  699. int absolute; /* offset is absolute address */
  700. int scsi_option;
  701. const struct esp_driver_ops *esp_ops;
  702. };
  703. /* board types */
  704. enum {
  705. ZORRO_BLZ1230,
  706. ZORRO_BLZ1230II,
  707. ZORRO_BLZ2060,
  708. ZORRO_CYBER,
  709. ZORRO_CYBERII,
  710. ZORRO_FASTLANE,
  711. };
  712. /* per-board config data */
  713. static const struct zorro_driver_data zorro_esp_boards[] = {
  714. [ZORRO_BLZ1230] = {
  715. .name = "Blizzard 1230",
  716. .offset = 0x8000,
  717. .dma_offset = 0x10000,
  718. .scsi_option = 1,
  719. .esp_ops = &blz1230_esp_ops,
  720. },
  721. [ZORRO_BLZ1230II] = {
  722. .name = "Blizzard 1230II",
  723. .offset = 0x10000,
  724. .dma_offset = 0x10021,
  725. .scsi_option = 1,
  726. .esp_ops = &blz1230II_esp_ops,
  727. },
  728. [ZORRO_BLZ2060] = {
  729. .name = "Blizzard 2060",
  730. .offset = 0x1ff00,
  731. .dma_offset = 0x1ffe0,
  732. .esp_ops = &blz2060_esp_ops,
  733. },
  734. [ZORRO_CYBER] = {
  735. .name = "CyberStormI",
  736. .offset = 0xf400,
  737. .dma_offset = 0xf800,
  738. .esp_ops = &cyber_esp_ops,
  739. },
  740. [ZORRO_CYBERII] = {
  741. .name = "CyberStormII",
  742. .offset = 0x1ff03,
  743. .dma_offset = 0x1ff43,
  744. .scsi_option = 1,
  745. .esp_ops = &cyberII_esp_ops,
  746. },
  747. [ZORRO_FASTLANE] = {
  748. .name = "Fastlane",
  749. .offset = 0x1000001,
  750. .dma_offset = 0x1000041,
  751. .esp_ops = &fastlane_esp_ops,
  752. },
  753. };
  754. static const struct zorro_device_id zorro_esp_zorro_tbl[] = {
  755. { /* Blizzard 1230 IV */
  756. .id = ZORRO_ID(PHASE5, 0x11, 0),
  757. .driver_data = ZORRO_BLZ1230,
  758. },
  759. { /* Blizzard 1230 II (Zorro II) or Fastlane (Zorro III) */
  760. .id = ZORRO_ID(PHASE5, 0x0B, 0),
  761. .driver_data = ZORRO_BLZ1230II,
  762. },
  763. { /* Blizzard 2060 */
  764. .id = ZORRO_ID(PHASE5, 0x18, 0),
  765. .driver_data = ZORRO_BLZ2060,
  766. },
  767. { /* Cyberstorm */
  768. .id = ZORRO_ID(PHASE5, 0x0C, 0),
  769. .driver_data = ZORRO_CYBER,
  770. },
  771. { /* Cyberstorm II */
  772. .id = ZORRO_ID(PHASE5, 0x19, 0),
  773. .driver_data = ZORRO_CYBERII,
  774. },
  775. { 0 }
  776. };
  777. MODULE_DEVICE_TABLE(zorro, zorro_esp_zorro_tbl);
  778. static int zorro_esp_probe(struct zorro_dev *z,
  779. const struct zorro_device_id *ent)
  780. {
  781. struct scsi_host_template *tpnt = &scsi_esp_template;
  782. struct Scsi_Host *host;
  783. struct esp *esp;
  784. const struct zorro_driver_data *zdd;
  785. struct zorro_esp_priv *zep;
  786. unsigned long board, ioaddr, dmaaddr;
  787. int err;
  788. board = zorro_resource_start(z);
  789. zdd = &zorro_esp_boards[ent->driver_data];
  790. pr_info("%s found at address 0x%lx.\n", zdd->name, board);
  791. zep = kzalloc(sizeof(*zep), GFP_KERNEL);
  792. if (!zep) {
  793. pr_err("Can't allocate device private data!\n");
  794. return -ENOMEM;
  795. }
  796. /* let's figure out whether we have a Zorro II or Zorro III board */
  797. if ((z->rom.er_Type & ERT_TYPEMASK) == ERT_ZORROIII) {
  798. if (board > 0xffffff)
  799. zep->zorro3 = 1;
  800. } else {
  801. /*
  802. * Even though most of these boards identify as Zorro II,
  803. * they are in fact CPU expansion slot boards and have full
  804. * access to all of memory. Fix up DMA bitmask here.
  805. */
  806. z->dev.coherent_dma_mask = DMA_BIT_MASK(32);
  807. }
  808. /*
  809. * If Zorro III and ID matches Fastlane, our device table entry
  810. * contains data for the Blizzard 1230 II board which does share the
  811. * same ID. Fix up device table entry here.
  812. * TODO: Some Cyberstom060 boards also share this ID but would need
  813. * to use the Cyberstorm I driver data ... we catch this by checking
  814. * for presence of ESP chip later, but don't try to fix up yet.
  815. */
  816. if (zep->zorro3 && ent->driver_data == ZORRO_BLZ1230II) {
  817. pr_info("%s at address 0x%lx is Fastlane Z3, fixing data!\n",
  818. zdd->name, board);
  819. zdd = &zorro_esp_boards[ZORRO_FASTLANE];
  820. }
  821. if (zdd->absolute) {
  822. ioaddr = zdd->offset;
  823. dmaaddr = zdd->dma_offset;
  824. } else {
  825. ioaddr = board + zdd->offset;
  826. dmaaddr = board + zdd->dma_offset;
  827. }
  828. if (!zorro_request_device(z, zdd->name)) {
  829. pr_err("cannot reserve region 0x%lx, abort\n",
  830. board);
  831. err = -EBUSY;
  832. goto fail_free_zep;
  833. }
  834. host = scsi_host_alloc(tpnt, sizeof(struct esp));
  835. if (!host) {
  836. pr_err("No host detected; board configuration problem?\n");
  837. err = -ENOMEM;
  838. goto fail_release_device;
  839. }
  840. host->base = ioaddr;
  841. host->this_id = 7;
  842. esp = shost_priv(host);
  843. esp->host = host;
  844. esp->dev = &z->dev;
  845. esp->scsi_id = host->this_id;
  846. esp->scsi_id_mask = (1 << esp->scsi_id);
  847. esp->cfreq = 40000000;
  848. zep->esp = esp;
  849. dev_set_drvdata(esp->dev, zep);
  850. /* additional setup required for Fastlane */
  851. if (zep->zorro3 && ent->driver_data == ZORRO_BLZ1230II) {
  852. /* map full address space up to ESP base for DMA */
  853. zep->board_base = ioremap_nocache(board,
  854. FASTLANE_ESP_ADDR-1);
  855. if (!zep->board_base) {
  856. pr_err("Cannot allocate board address space\n");
  857. err = -ENOMEM;
  858. goto fail_free_host;
  859. }
  860. /* initialize DMA control shadow register */
  861. zep->ctrl_data = (FASTLANE_DMA_FCODE |
  862. FASTLANE_DMA_EDI | FASTLANE_DMA_ESI);
  863. }
  864. esp->ops = zdd->esp_ops;
  865. if (ioaddr > 0xffffff)
  866. esp->regs = ioremap_nocache(ioaddr, 0x20);
  867. else
  868. /* ZorroII address space remapped nocache by early startup */
  869. esp->regs = ZTWO_VADDR(ioaddr);
  870. if (!esp->regs) {
  871. err = -ENOMEM;
  872. goto fail_unmap_fastlane;
  873. }
  874. /* Check whether a Blizzard 12x0 or CyberstormII really has SCSI */
  875. if (zdd->scsi_option) {
  876. zorro_esp_write8(esp, (ESP_CONFIG1_PENABLE | 7), ESP_CFG1);
  877. if (zorro_esp_read8(esp, ESP_CFG1) != (ESP_CONFIG1_PENABLE|7)) {
  878. err = -ENODEV;
  879. goto fail_unmap_regs;
  880. }
  881. }
  882. if (zep->zorro3) {
  883. /*
  884. * Only Fastlane Z3 for now - add switch for correct struct
  885. * dma_registers size if adding any more
  886. */
  887. esp->dma_regs = ioremap_nocache(dmaaddr,
  888. sizeof(struct fastlane_dma_registers));
  889. } else
  890. /* ZorroII address space remapped nocache by early startup */
  891. esp->dma_regs = ZTWO_VADDR(dmaaddr);
  892. if (!esp->dma_regs) {
  893. err = -ENOMEM;
  894. goto fail_unmap_regs;
  895. }
  896. esp->command_block = dma_alloc_coherent(esp->dev, 16,
  897. &esp->command_block_dma,
  898. GFP_KERNEL);
  899. if (!esp->command_block) {
  900. err = -ENOMEM;
  901. goto fail_unmap_dma_regs;
  902. }
  903. host->irq = IRQ_AMIGA_PORTS;
  904. err = request_irq(host->irq, scsi_esp_intr, IRQF_SHARED,
  905. "Amiga Zorro ESP", esp);
  906. if (err < 0) {
  907. err = -ENODEV;
  908. goto fail_free_command_block;
  909. }
  910. /* register the chip */
  911. err = scsi_esp_register(esp, &z->dev);
  912. if (err) {
  913. err = -ENOMEM;
  914. goto fail_free_irq;
  915. }
  916. return 0;
  917. fail_free_irq:
  918. free_irq(host->irq, esp);
  919. fail_free_command_block:
  920. dma_free_coherent(esp->dev, 16,
  921. esp->command_block,
  922. esp->command_block_dma);
  923. fail_unmap_dma_regs:
  924. if (zep->zorro3)
  925. iounmap(esp->dma_regs);
  926. fail_unmap_regs:
  927. if (ioaddr > 0xffffff)
  928. iounmap(esp->regs);
  929. fail_unmap_fastlane:
  930. if (zep->zorro3)
  931. iounmap(zep->board_base);
  932. fail_free_host:
  933. scsi_host_put(host);
  934. fail_release_device:
  935. zorro_release_device(z);
  936. fail_free_zep:
  937. kfree(zep);
  938. return err;
  939. }
  940. static void zorro_esp_remove(struct zorro_dev *z)
  941. {
  942. struct zorro_esp_priv *zep = dev_get_drvdata(&z->dev);
  943. struct esp *esp = zep->esp;
  944. struct Scsi_Host *host = esp->host;
  945. scsi_esp_unregister(esp);
  946. free_irq(host->irq, esp);
  947. dma_free_coherent(esp->dev, 16,
  948. esp->command_block,
  949. esp->command_block_dma);
  950. if (zep->zorro3) {
  951. iounmap(zep->board_base);
  952. iounmap(esp->dma_regs);
  953. }
  954. if (host->base > 0xffffff)
  955. iounmap(esp->regs);
  956. scsi_host_put(host);
  957. zorro_release_device(z);
  958. kfree(zep);
  959. }
  960. static struct zorro_driver zorro_esp_driver = {
  961. .name = KBUILD_MODNAME,
  962. .id_table = zorro_esp_zorro_tbl,
  963. .probe = zorro_esp_probe,
  964. .remove = zorro_esp_remove,
  965. };
  966. static int __init zorro_esp_scsi_init(void)
  967. {
  968. return zorro_register_driver(&zorro_esp_driver);
  969. }
  970. static void __exit zorro_esp_scsi_exit(void)
  971. {
  972. zorro_unregister_driver(&zorro_esp_driver);
  973. }
  974. module_init(zorro_esp_scsi_init);
  975. module_exit(zorro_esp_scsi_exit);