mca_53c9x.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520
  1. /* mca_53c9x.c: Driver for the SCSI adapter found on NCR 35xx
  2. * (and maybe some other) Microchannel machines
  3. *
  4. * Code taken mostly from Cyberstorm SCSI drivers
  5. * Copyright (C) 1996 Jesper Skov (jskov@cygnus.co.uk)
  6. *
  7. * Hacked to work with the NCR MCA stuff by Tymm Twillman (tymm@computer.org)
  8. *
  9. * The CyberStorm SCSI driver (and this driver) is based on David S. Miller's
  10. * ESP driver * for the Sparc computers.
  11. *
  12. * Special thanks to Ken Stewart at Symbios (LSI) for helping with info on
  13. * the 86C01. I was on the brink of going ga-ga...
  14. *
  15. * Also thanks to Jesper Skov for helping me with info on how the Amiga
  16. * does things...
  17. */
  18. /*
  19. * This is currently only set up to use one 53c9x card at a time; it could be
  20. * changed fairly easily to detect/use more than one, but I'm not too sure how
  21. * many cards that use the 53c9x on MCA systems there are (if, in fact, there
  22. * are cards that use them, other than the one built into some NCR systems)...
  23. * If anyone requests this, I'll throw it in, otherwise it's not worth the
  24. * effort.
  25. */
  26. /*
  27. * Info on the 86C01 MCA interface chip at the bottom, if you care enough to
  28. * look.
  29. */
  30. #include <linux/delay.h>
  31. #include <linux/interrupt.h>
  32. #include <linux/kernel.h>
  33. #include <linux/mca.h>
  34. #include <linux/types.h>
  35. #include <linux/string.h>
  36. #include <linux/slab.h>
  37. #include <linux/blkdev.h>
  38. #include <linux/proc_fs.h>
  39. #include <linux/stat.h>
  40. #include <linux/mca-legacy.h>
  41. #include "scsi.h"
  42. #include <scsi/scsi_host.h>
  43. #include "NCR53C9x.h"
  44. #include <asm/dma.h>
  45. #include <asm/irq.h>
  46. #include <asm/mca_dma.h>
  47. #include <asm/pgtable.h>
  48. /*
  49. * From ibmmca.c (IBM scsi controller card driver) -- used for turning PS2 disk
  50. * activity LED on and off
  51. */
  52. #define PS2_SYS_CTR 0x92
  53. /* Ports the ncr's 53c94 can be put at; indexed by pos register value */
  54. #define MCA_53C9X_IO_PORTS { \
  55. 0x0000, 0x0240, 0x0340, 0x0400, \
  56. 0x0420, 0x3240, 0x8240, 0xA240, \
  57. }
  58. /*
  59. * Supposedly there were some cards put together with the 'c9x and 86c01. If
  60. * they have different ID's from the ones on the 3500 series machines,
  61. * you can add them here and hopefully things will work out.
  62. */
  63. #define MCA_53C9X_IDS { \
  64. 0x7F4C, \
  65. 0x0000, \
  66. }
  67. static int dma_bytes_sent(struct NCR_ESP *, int);
  68. static int dma_can_transfer(struct NCR_ESP *, Scsi_Cmnd *);
  69. static void dma_dump_state(struct NCR_ESP *);
  70. static void dma_init_read(struct NCR_ESP *, __u32, int);
  71. static void dma_init_write(struct NCR_ESP *, __u32, int);
  72. static void dma_ints_off(struct NCR_ESP *);
  73. static void dma_ints_on(struct NCR_ESP *);
  74. static int dma_irq_p(struct NCR_ESP *);
  75. static int dma_ports_p(struct NCR_ESP *);
  76. static void dma_setup(struct NCR_ESP *, __u32, int, int);
  77. static void dma_led_on(struct NCR_ESP *);
  78. static void dma_led_off(struct NCR_ESP *);
  79. /* This is where all commands are put before they are trasfered to the
  80. * 53c9x via PIO.
  81. */
  82. static volatile unsigned char cmd_buffer[16];
  83. /*
  84. * We keep the structure that is used to access the registers on the 53c9x
  85. * here.
  86. */
  87. static struct ESP_regs eregs;
  88. /***************************************************************** Detection */
  89. static int mca_esp_detect(struct scsi_host_template *tpnt)
  90. {
  91. struct NCR_ESP *esp;
  92. static int io_port_by_pos[] = MCA_53C9X_IO_PORTS;
  93. int mca_53c9x_ids[] = MCA_53C9X_IDS;
  94. int *id_to_check = mca_53c9x_ids;
  95. int slot;
  96. int pos[3];
  97. unsigned int tmp_io_addr;
  98. unsigned char tmp_byte;
  99. if (!MCA_bus)
  100. return 0;
  101. while (*id_to_check) {
  102. if ((slot = mca_find_adapter(*id_to_check, 0)) !=
  103. MCA_NOTFOUND)
  104. {
  105. esp = esp_allocate(tpnt, (void *) NULL);
  106. pos[0] = mca_read_stored_pos(slot, 2);
  107. pos[1] = mca_read_stored_pos(slot, 3);
  108. pos[2] = mca_read_stored_pos(slot, 4);
  109. esp->eregs = &eregs;
  110. /*
  111. * IO port base is given in the first (non-ID) pos
  112. * register, like so:
  113. *
  114. * Bits 3 2 1 IO base
  115. * ----------------------------
  116. * 0 0 0 <disabled>
  117. * 0 0 1 0x0240
  118. * 0 1 0 0x0340
  119. * 0 1 1 0x0400
  120. * 1 0 0 0x0420
  121. * 1 0 1 0x3240
  122. * 1 1 0 0x8240
  123. * 1 1 1 0xA240
  124. */
  125. tmp_io_addr =
  126. io_port_by_pos[(pos[0] & 0x0E) >> 1];
  127. esp->eregs->io_addr = tmp_io_addr + 0x10;
  128. if (esp->eregs->io_addr == 0x0000) {
  129. printk("Adapter is disabled.\n");
  130. break;
  131. }
  132. /*
  133. * IRQ is specified in bits 4 and 5:
  134. *
  135. * Bits 4 5 IRQ
  136. * -----------------------
  137. * 0 0 3
  138. * 0 1 5
  139. * 1 0 7
  140. * 1 1 9
  141. */
  142. esp->irq = ((pos[0] & 0x30) >> 3) + 3;
  143. /*
  144. * DMA channel is in the low 3 bits of the second
  145. * POS register
  146. */
  147. esp->dma = pos[1] & 7;
  148. esp->slot = slot;
  149. if (request_irq(esp->irq, esp_intr, 0,
  150. "NCR 53c9x SCSI", esp->ehost))
  151. {
  152. printk("Unable to request IRQ %d.\n", esp->irq);
  153. esp_deallocate(esp);
  154. scsi_unregister(esp->ehost);
  155. return 0;
  156. }
  157. if (request_dma(esp->dma, "NCR 53c9x SCSI")) {
  158. printk("Unable to request DMA channel %d.\n",
  159. esp->dma);
  160. free_irq(esp->irq, esp_intr);
  161. esp_deallocate(esp);
  162. scsi_unregister(esp->ehost);
  163. return 0;
  164. }
  165. request_region(tmp_io_addr, 32, "NCR 53c9x SCSI");
  166. /*
  167. * 86C01 handles DMA, IO mode, from address
  168. * (base + 0x0a)
  169. */
  170. mca_disable_dma(esp->dma);
  171. mca_set_dma_io(esp->dma, tmp_io_addr + 0x0a);
  172. mca_enable_dma(esp->dma);
  173. /* Tell the 86C01 to give us interrupts */
  174. tmp_byte = inb(tmp_io_addr + 0x02) | 0x40;
  175. outb(tmp_byte, tmp_io_addr + 0x02);
  176. /*
  177. * Scsi ID -- general purpose register, hi
  178. * 2 bits; add 4 to this number to get the
  179. * ID
  180. */
  181. esp->scsi_id = ((pos[2] & 0xC0) >> 6) + 4;
  182. /* Do command transfer with programmed I/O */
  183. esp->do_pio_cmds = 1;
  184. /* Required functions */
  185. esp->dma_bytes_sent = &dma_bytes_sent;
  186. esp->dma_can_transfer = &dma_can_transfer;
  187. esp->dma_dump_state = &dma_dump_state;
  188. esp->dma_init_read = &dma_init_read;
  189. esp->dma_init_write = &dma_init_write;
  190. esp->dma_ints_off = &dma_ints_off;
  191. esp->dma_ints_on = &dma_ints_on;
  192. esp->dma_irq_p = &dma_irq_p;
  193. esp->dma_ports_p = &dma_ports_p;
  194. esp->dma_setup = &dma_setup;
  195. /* Optional functions */
  196. esp->dma_barrier = NULL;
  197. esp->dma_drain = NULL;
  198. esp->dma_invalidate = NULL;
  199. esp->dma_irq_entry = NULL;
  200. esp->dma_irq_exit = NULL;
  201. esp->dma_led_on = dma_led_on;
  202. esp->dma_led_off = dma_led_off;
  203. esp->dma_poll = NULL;
  204. esp->dma_reset = NULL;
  205. /* Set the command buffer */
  206. esp->esp_command = (volatile unsigned char*)
  207. cmd_buffer;
  208. esp->esp_command_dvma = isa_virt_to_bus(cmd_buffer);
  209. /* SCSI chip speed */
  210. esp->cfreq = 25000000;
  211. /* Differential SCSI? I think not. */
  212. esp->diff = 0;
  213. esp_initialize(esp);
  214. printk(" Adapter found in slot %2d: io port 0x%x "
  215. "irq %d dma channel %d\n", slot + 1, tmp_io_addr,
  216. esp->irq, esp->dma);
  217. mca_set_adapter_name(slot, "NCR 53C9X SCSI Adapter");
  218. mca_mark_as_used(slot);
  219. break;
  220. }
  221. id_to_check++;
  222. }
  223. return esps_in_use;
  224. }
  225. /******************************************************************* Release */
  226. static int mca_esp_release(struct Scsi_Host *host)
  227. {
  228. struct NCR_ESP *esp = (struct NCR_ESP *)host->hostdata;
  229. unsigned char tmp_byte;
  230. esp_deallocate(esp);
  231. /*
  232. * Tell the 86C01 to stop sending interrupts
  233. */
  234. tmp_byte = inb(esp->eregs->io_addr - 0x0E);
  235. tmp_byte &= ~0x40;
  236. outb(tmp_byte, esp->eregs->io_addr - 0x0E);
  237. free_irq(esp->irq, esp_intr);
  238. free_dma(esp->dma);
  239. mca_mark_as_unused(esp->slot);
  240. return 0;
  241. }
  242. /************************************************************* DMA Functions */
  243. static int dma_bytes_sent(struct NCR_ESP *esp, int fifo_count)
  244. {
  245. /* Ask the 53c9x. It knows. */
  246. return fifo_count;
  247. }
  248. static int dma_can_transfer(struct NCR_ESP *esp, Scsi_Cmnd *sp)
  249. {
  250. /*
  251. * The MCA dma channels can only do up to 128K bytes at a time.
  252. * (16 bit mode)
  253. */
  254. unsigned long sz = sp->SCp.this_residual;
  255. if(sz > 0x20000)
  256. sz = 0x20000;
  257. return sz;
  258. }
  259. static void dma_dump_state(struct NCR_ESP *esp)
  260. {
  261. /*
  262. * Doesn't quite match up to the other drivers, but we do what we
  263. * can.
  264. */
  265. ESPLOG(("esp%d: dma channel <%d>\n", esp->esp_id, esp->dma));
  266. ESPLOG(("bytes left to dma: %d\n", mca_get_dma_residue(esp->dma)));
  267. }
  268. static void dma_init_read(struct NCR_ESP *esp, __u32 addr, int length)
  269. {
  270. unsigned long flags;
  271. save_flags(flags);
  272. cli();
  273. mca_disable_dma(esp->dma);
  274. mca_set_dma_mode(esp->dma, MCA_DMA_MODE_XFER | MCA_DMA_MODE_16 |
  275. MCA_DMA_MODE_IO);
  276. mca_set_dma_addr(esp->dma, addr);
  277. mca_set_dma_count(esp->dma, length / 2); /* !!! */
  278. mca_enable_dma(esp->dma);
  279. restore_flags(flags);
  280. }
  281. static void dma_init_write(struct NCR_ESP *esp, __u32 addr, int length)
  282. {
  283. unsigned long flags;
  284. save_flags(flags);
  285. cli();
  286. mca_disable_dma(esp->dma);
  287. mca_set_dma_mode(esp->dma, MCA_DMA_MODE_XFER | MCA_DMA_MODE_WRITE |
  288. MCA_DMA_MODE_16 | MCA_DMA_MODE_IO);
  289. mca_set_dma_addr(esp->dma, addr);
  290. mca_set_dma_count(esp->dma, length / 2); /* !!! */
  291. mca_enable_dma(esp->dma);
  292. restore_flags(flags);
  293. }
  294. static void dma_ints_off(struct NCR_ESP *esp)
  295. {
  296. /*
  297. * Tell the 'C01 to shut up. All interrupts are routed through it.
  298. */
  299. outb(inb(esp->eregs->io_addr - 0x0E) & ~0x40,
  300. esp->eregs->io_addr - 0x0E);
  301. }
  302. static void dma_ints_on(struct NCR_ESP *esp)
  303. {
  304. /*
  305. * Ok. You can speak again.
  306. */
  307. outb(inb(esp->eregs->io_addr - 0x0E) | 0x40,
  308. esp->eregs->io_addr - 0x0E);
  309. }
  310. static int dma_irq_p(struct NCR_ESP *esp)
  311. {
  312. /*
  313. * DaveM says that this should return a "yes" if there is an interrupt
  314. * or a DMA error occurred. I copied the Amiga driver's semantics,
  315. * though, because it seems to work and we can't really tell if
  316. * a DMA error happened. This gives the "yes" if the scsi chip
  317. * is sending an interrupt and no DMA activity is taking place
  318. */
  319. return (!(inb(esp->eregs->io_addr - 0x04) & 1) &&
  320. !(inb(esp->eregs->io_addr - 0x04) & 2) );
  321. }
  322. static int dma_ports_p(struct NCR_ESP *esp)
  323. {
  324. /*
  325. * Check to see if interrupts are enabled on the 'C01 (in case abort
  326. * is entered multiple times, so we only do the abort once)
  327. */
  328. return (inb(esp->eregs->io_addr - 0x0E) & 0x40) ? 1:0;
  329. }
  330. static void dma_setup(struct NCR_ESP *esp, __u32 addr, int count, int write)
  331. {
  332. if(write){
  333. dma_init_write(esp, addr, count);
  334. } else {
  335. dma_init_read(esp, addr, count);
  336. }
  337. }
  338. /*
  339. * These will not play nicely with other disk controllers that try to use the
  340. * disk active LED... but what can you do? Don't answer that.
  341. *
  342. * Stolen shamelessly from ibmmca.c -- IBM Microchannel SCSI adapter driver
  343. *
  344. */
  345. static void dma_led_on(struct NCR_ESP *esp)
  346. {
  347. outb(inb(PS2_SYS_CTR) | 0xc0, PS2_SYS_CTR);
  348. }
  349. static void dma_led_off(struct NCR_ESP *esp)
  350. {
  351. outb(inb(PS2_SYS_CTR) & 0x3f, PS2_SYS_CTR);
  352. }
  353. static struct scsi_host_template driver_template = {
  354. .proc_name = "mca_53c9x",
  355. .name = "NCR 53c9x SCSI",
  356. .detect = mca_esp_detect,
  357. .slave_alloc = esp_slave_alloc,
  358. .slave_destroy = esp_slave_destroy,
  359. .release = mca_esp_release,
  360. .queuecommand = esp_queue,
  361. .eh_abort_handler = esp_abort,
  362. .eh_bus_reset_handler = esp_reset,
  363. .can_queue = 7,
  364. .sg_tablesize = SG_ALL,
  365. .cmd_per_lun = 1,
  366. .unchecked_isa_dma = 1,
  367. .use_clustering = DISABLE_CLUSTERING
  368. };
  369. #include "scsi_module.c"
  370. /*
  371. * OK, here's the goods I promised. The NCR 86C01 is an MCA interface chip
  372. * that handles enabling/diabling IRQ, dma interfacing, IO port selection
  373. * and other fun stuff. It takes up 16 addresses, and the chip it is
  374. * connnected to gets the following 16. Registers are as follows:
  375. *
  376. * Offsets 0-1 : Card ID
  377. *
  378. * Offset 2 : Mode enable register --
  379. * Bit 7 : Data Word width (1 = 16, 0 = 8)
  380. * Bit 6 : IRQ enable (1 = enabled)
  381. * Bits 5,4 : IRQ select
  382. * 0 0 : IRQ 3
  383. * 0 1 : IRQ 5
  384. * 1 0 : IRQ 7
  385. * 1 1 : IRQ 9
  386. * Bits 3-1 : Base Address
  387. * 0 0 0 : <disabled>
  388. * 0 0 1 : 0x0240
  389. * 0 1 0 : 0x0340
  390. * 0 1 1 : 0x0400
  391. * 1 0 0 : 0x0420
  392. * 1 0 1 : 0x3240
  393. * 1 1 0 : 0x8240
  394. * 1 1 1 : 0xA240
  395. * Bit 0 : Card enable (1 = enabled)
  396. *
  397. * Offset 3 : DMA control register --
  398. * Bit 7 : DMA enable (1 = enabled)
  399. * Bits 6,5 : Preemt Count Select (transfers to complete after
  400. * 'C01 has been preempted on MCA bus)
  401. * 0 0 : 0
  402. * 0 1 : 1
  403. * 1 0 : 3
  404. * 1 1 : 7
  405. * (all these wacky numbers; I'm sure there's a reason somewhere)
  406. * Bit 4 : Fairness enable (1 = fair bus priority)
  407. * Bits 3-0 : Arbitration level (0-15 consecutive)
  408. *
  409. * Offset 4 : General purpose register
  410. * Bits 7-3 : User definable (here, 7,6 are SCSI ID)
  411. * Bits 2-0 : reserved
  412. *
  413. * Offset 10 : DMA decode register (used for IO based DMA; also can do
  414. * PIO through this port)
  415. *
  416. * Offset 12 : Status
  417. * Bits 7-2 : reserved
  418. * Bit 1 : DMA pending (1 = pending)
  419. * Bit 0 : IRQ pending (0 = pending)
  420. *
  421. * Exciting, huh?
  422. *
  423. */