cumana_1.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Generic Generic NCR5380 driver
  4. *
  5. * Copyright 1995-2002, Russell King
  6. */
  7. #include <linux/module.h>
  8. #include <linux/ioport.h>
  9. #include <linux/blkdev.h>
  10. #include <linux/init.h>
  11. #include <asm/ecard.h>
  12. #include <asm/io.h>
  13. #include <scsi/scsi_host.h>
  14. #define priv(host) ((struct NCR5380_hostdata *)(host)->hostdata)
  15. #define NCR5380_read(reg) cumanascsi_read(hostdata, reg)
  16. #define NCR5380_write(reg, value) cumanascsi_write(hostdata, reg, value)
  17. #define NCR5380_dma_xfer_len cumanascsi_dma_xfer_len
  18. #define NCR5380_dma_recv_setup cumanascsi_pread
  19. #define NCR5380_dma_send_setup cumanascsi_pwrite
  20. #define NCR5380_dma_residual NCR5380_dma_residual_none
  21. #define NCR5380_intr cumanascsi_intr
  22. #define NCR5380_queue_command cumanascsi_queue_command
  23. #define NCR5380_info cumanascsi_info
  24. #define NCR5380_implementation_fields \
  25. unsigned ctrl
  26. struct NCR5380_hostdata;
  27. static u8 cumanascsi_read(struct NCR5380_hostdata *, unsigned int);
  28. static void cumanascsi_write(struct NCR5380_hostdata *, unsigned int, u8);
  29. #include "../NCR5380.h"
  30. #define CTRL 0x16fc
  31. #define STAT 0x2004
  32. #define L(v) (((v)<<16)|((v) & 0x0000ffff))
  33. #define H(v) (((v)>>16)|((v) & 0xffff0000))
  34. static inline int cumanascsi_pwrite(struct NCR5380_hostdata *hostdata,
  35. unsigned char *addr, int len)
  36. {
  37. unsigned long *laddr;
  38. u8 __iomem *base = hostdata->io;
  39. u8 __iomem *dma = hostdata->pdma_io + 0x2000;
  40. if(!len) return 0;
  41. writeb(0x02, base + CTRL);
  42. laddr = (unsigned long *)addr;
  43. while(len >= 32)
  44. {
  45. unsigned int status;
  46. unsigned long v;
  47. status = readb(base + STAT);
  48. if(status & 0x80)
  49. goto end;
  50. if(!(status & 0x40))
  51. continue;
  52. v=*laddr++; writew(L(v), dma); writew(H(v), dma);
  53. v=*laddr++; writew(L(v), dma); writew(H(v), dma);
  54. v=*laddr++; writew(L(v), dma); writew(H(v), dma);
  55. v=*laddr++; writew(L(v), dma); writew(H(v), dma);
  56. v=*laddr++; writew(L(v), dma); writew(H(v), dma);
  57. v=*laddr++; writew(L(v), dma); writew(H(v), dma);
  58. v=*laddr++; writew(L(v), dma); writew(H(v), dma);
  59. v=*laddr++; writew(L(v), dma); writew(H(v), dma);
  60. len -= 32;
  61. if(len == 0)
  62. break;
  63. }
  64. addr = (unsigned char *)laddr;
  65. writeb(0x12, base + CTRL);
  66. while(len > 0)
  67. {
  68. unsigned int status;
  69. status = readb(base + STAT);
  70. if(status & 0x80)
  71. goto end;
  72. if(status & 0x40)
  73. {
  74. writeb(*addr++, dma);
  75. if(--len == 0)
  76. break;
  77. }
  78. status = readb(base + STAT);
  79. if(status & 0x80)
  80. goto end;
  81. if(status & 0x40)
  82. {
  83. writeb(*addr++, dma);
  84. if(--len == 0)
  85. break;
  86. }
  87. }
  88. end:
  89. writeb(hostdata->ctrl | 0x40, base + CTRL);
  90. if (len)
  91. return -1;
  92. return 0;
  93. }
  94. static inline int cumanascsi_pread(struct NCR5380_hostdata *hostdata,
  95. unsigned char *addr, int len)
  96. {
  97. unsigned long *laddr;
  98. u8 __iomem *base = hostdata->io;
  99. u8 __iomem *dma = hostdata->pdma_io + 0x2000;
  100. if(!len) return 0;
  101. writeb(0x00, base + CTRL);
  102. laddr = (unsigned long *)addr;
  103. while(len >= 32)
  104. {
  105. unsigned int status;
  106. status = readb(base + STAT);
  107. if(status & 0x80)
  108. goto end;
  109. if(!(status & 0x40))
  110. continue;
  111. *laddr++ = readw(dma) | (readw(dma) << 16);
  112. *laddr++ = readw(dma) | (readw(dma) << 16);
  113. *laddr++ = readw(dma) | (readw(dma) << 16);
  114. *laddr++ = readw(dma) | (readw(dma) << 16);
  115. *laddr++ = readw(dma) | (readw(dma) << 16);
  116. *laddr++ = readw(dma) | (readw(dma) << 16);
  117. *laddr++ = readw(dma) | (readw(dma) << 16);
  118. *laddr++ = readw(dma) | (readw(dma) << 16);
  119. len -= 32;
  120. if(len == 0)
  121. break;
  122. }
  123. addr = (unsigned char *)laddr;
  124. writeb(0x10, base + CTRL);
  125. while(len > 0)
  126. {
  127. unsigned int status;
  128. status = readb(base + STAT);
  129. if(status & 0x80)
  130. goto end;
  131. if(status & 0x40)
  132. {
  133. *addr++ = readb(dma);
  134. if(--len == 0)
  135. break;
  136. }
  137. status = readb(base + STAT);
  138. if(status & 0x80)
  139. goto end;
  140. if(status & 0x40)
  141. {
  142. *addr++ = readb(dma);
  143. if(--len == 0)
  144. break;
  145. }
  146. }
  147. end:
  148. writeb(hostdata->ctrl | 0x40, base + CTRL);
  149. if (len)
  150. return -1;
  151. return 0;
  152. }
  153. static int cumanascsi_dma_xfer_len(struct NCR5380_hostdata *hostdata,
  154. struct scsi_cmnd *cmd)
  155. {
  156. return cmd->transfersize;
  157. }
  158. static u8 cumanascsi_read(struct NCR5380_hostdata *hostdata,
  159. unsigned int reg)
  160. {
  161. u8 __iomem *base = hostdata->io;
  162. u8 val;
  163. writeb(0, base + CTRL);
  164. val = readb(base + 0x2100 + (reg << 2));
  165. hostdata->ctrl = 0x40;
  166. writeb(0x40, base + CTRL);
  167. return val;
  168. }
  169. static void cumanascsi_write(struct NCR5380_hostdata *hostdata,
  170. unsigned int reg, u8 value)
  171. {
  172. u8 __iomem *base = hostdata->io;
  173. writeb(0, base + CTRL);
  174. writeb(value, base + 0x2100 + (reg << 2));
  175. hostdata->ctrl = 0x40;
  176. writeb(0x40, base + CTRL);
  177. }
  178. #include "../NCR5380.c"
  179. static struct scsi_host_template cumanascsi_template = {
  180. .module = THIS_MODULE,
  181. .name = "Cumana 16-bit SCSI",
  182. .info = cumanascsi_info,
  183. .queuecommand = cumanascsi_queue_command,
  184. .eh_abort_handler = NCR5380_abort,
  185. .eh_host_reset_handler = NCR5380_host_reset,
  186. .can_queue = 16,
  187. .this_id = 7,
  188. .sg_tablesize = SG_ALL,
  189. .cmd_per_lun = 2,
  190. .proc_name = "CumanaSCSI-1",
  191. .cmd_size = sizeof(struct NCR5380_cmd),
  192. .max_sectors = 128,
  193. .dma_boundary = PAGE_SIZE - 1,
  194. };
  195. static int cumanascsi1_probe(struct expansion_card *ec,
  196. const struct ecard_id *id)
  197. {
  198. struct Scsi_Host *host;
  199. int ret;
  200. ret = ecard_request_resources(ec);
  201. if (ret)
  202. goto out;
  203. host = scsi_host_alloc(&cumanascsi_template, sizeof(struct NCR5380_hostdata));
  204. if (!host) {
  205. ret = -ENOMEM;
  206. goto out_release;
  207. }
  208. priv(host)->io = ioremap(ecard_resource_start(ec, ECARD_RES_IOCSLOW),
  209. ecard_resource_len(ec, ECARD_RES_IOCSLOW));
  210. priv(host)->pdma_io = ioremap(ecard_resource_start(ec, ECARD_RES_MEMC),
  211. ecard_resource_len(ec, ECARD_RES_MEMC));
  212. if (!priv(host)->io || !priv(host)->pdma_io) {
  213. ret = -ENOMEM;
  214. goto out_unmap;
  215. }
  216. host->irq = ec->irq;
  217. ret = NCR5380_init(host, FLAG_DMA_FIXUP | FLAG_LATE_DMA_SETUP);
  218. if (ret)
  219. goto out_unmap;
  220. NCR5380_maybe_reset_bus(host);
  221. priv(host)->ctrl = 0;
  222. writeb(0, priv(host)->io + CTRL);
  223. ret = request_irq(host->irq, cumanascsi_intr, 0,
  224. "CumanaSCSI-1", host);
  225. if (ret) {
  226. printk("scsi%d: IRQ%d not free: %d\n",
  227. host->host_no, host->irq, ret);
  228. goto out_exit;
  229. }
  230. ret = scsi_add_host(host, &ec->dev);
  231. if (ret)
  232. goto out_free_irq;
  233. scsi_scan_host(host);
  234. goto out;
  235. out_free_irq:
  236. free_irq(host->irq, host);
  237. out_exit:
  238. NCR5380_exit(host);
  239. out_unmap:
  240. iounmap(priv(host)->io);
  241. iounmap(priv(host)->pdma_io);
  242. scsi_host_put(host);
  243. out_release:
  244. ecard_release_resources(ec);
  245. out:
  246. return ret;
  247. }
  248. static void cumanascsi1_remove(struct expansion_card *ec)
  249. {
  250. struct Scsi_Host *host = ecard_get_drvdata(ec);
  251. void __iomem *base = priv(host)->io;
  252. void __iomem *dma = priv(host)->pdma_io;
  253. ecard_set_drvdata(ec, NULL);
  254. scsi_remove_host(host);
  255. free_irq(host->irq, host);
  256. NCR5380_exit(host);
  257. scsi_host_put(host);
  258. iounmap(base);
  259. iounmap(dma);
  260. ecard_release_resources(ec);
  261. }
  262. static const struct ecard_id cumanascsi1_cids[] = {
  263. { MANU_CUMANA, PROD_CUMANA_SCSI_1 },
  264. { 0xffff, 0xffff }
  265. };
  266. static struct ecard_driver cumanascsi1_driver = {
  267. .probe = cumanascsi1_probe,
  268. .remove = cumanascsi1_remove,
  269. .id_table = cumanascsi1_cids,
  270. .drv = {
  271. .name = "cumanascsi1",
  272. },
  273. };
  274. static int __init cumanascsi_init(void)
  275. {
  276. return ecard_register_driver(&cumanascsi1_driver);
  277. }
  278. static void __exit cumanascsi_exit(void)
  279. {
  280. ecard_remove_driver(&cumanascsi1_driver);
  281. }
  282. module_init(cumanascsi_init);
  283. module_exit(cumanascsi_exit);
  284. MODULE_DESCRIPTION("Cumana SCSI-1 driver for Acorn machines");
  285. MODULE_LICENSE("GPL");