zorro_esp.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173
  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);