NCR53c406a.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101
  1. /*
  2. * NCR53c406.c
  3. * Low-level SCSI driver for NCR53c406a chip.
  4. * Copyright (C) 1994, 1995, 1996 Normunds Saumanis (normunds@fi.ibm.com)
  5. *
  6. * LILO command line usage: ncr53c406a=<PORTBASE>[,<IRQ>[,<FASTPIO>]]
  7. * Specify IRQ = 0 for non-interrupt driven mode.
  8. * FASTPIO = 1 for fast pio mode, 0 for slow mode.
  9. *
  10. * This program is free software; you can redistribute it and/or modify it
  11. * under the terms of the GNU General Public License as published by the
  12. * Free Software Foundation; either version 2, or (at your option) any
  13. * later version.
  14. *
  15. * This program is distributed in the hope that it will be useful, but
  16. * WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. * General Public License for more details.
  19. *
  20. */
  21. #define NCR53C406A_DEBUG 0
  22. #define VERBOSE_NCR53C406A_DEBUG 0
  23. /* Set this to 1 for PIO mode (recommended) or to 0 for DMA mode */
  24. #define USE_PIO 1
  25. #define USE_BIOS 0
  26. /* #define BIOS_ADDR 0xD8000 *//* define this if autoprobe fails */
  27. /* #define PORT_BASE 0x330 *//* define this if autoprobe fails */
  28. /* #define IRQ_LEV 0 *//* define this if autoprobe fails */
  29. #define DMA_CHAN 5 /* this is ignored if DMA is disabled */
  30. /* Set this to 0 if you encounter kernel lockups while transferring
  31. * data in PIO mode */
  32. #define USE_FAST_PIO 1
  33. /* ============= End of user configurable parameters ============= */
  34. #include <linux/module.h>
  35. #include <linux/errno.h>
  36. #include <linux/ioport.h>
  37. #include <linux/sched.h>
  38. #include <linux/interrupt.h>
  39. #include <linux/proc_fs.h>
  40. #include <linux/stat.h>
  41. #include <linux/init.h>
  42. #include <linux/bitops.h>
  43. #include <asm/io.h>
  44. #include <asm/dma.h>
  45. #include <asm/irq.h>
  46. #include <linux/blkdev.h>
  47. #include <linux/spinlock.h>
  48. #include "scsi.h"
  49. #include <scsi/scsi_host.h>
  50. /* ============================================================= */
  51. #define WATCHDOG 5000000
  52. #define SYNC_MODE 0 /* Synchronous transfer mode */
  53. #ifdef DEBUG
  54. #undef NCR53C406A_DEBUG
  55. #define NCR53C406A_DEBUG 1
  56. #endif
  57. #if USE_PIO
  58. #define USE_DMA 0
  59. #else
  60. #define USE_DMA 1
  61. #endif
  62. /* Default configuration */
  63. #define C1_IMG 0x07 /* ID=7 */
  64. #define C2_IMG 0x48 /* FE SCSI2 */
  65. #if USE_DMA
  66. #define C3_IMG 0x21 /* CDB TE */
  67. #else
  68. #define C3_IMG 0x20 /* CDB */
  69. #endif
  70. #define C4_IMG 0x04 /* ANE */
  71. #define C5_IMG 0xb6 /* AA PI SIE POL */
  72. #define REG0 (outb(C4_IMG, CONFIG4))
  73. #define REG1 (outb(C5_IMG, CONFIG5))
  74. #if NCR53C406A_DEBUG
  75. #define DEB(x) x
  76. #else
  77. #define DEB(x)
  78. #endif
  79. #if VERBOSE_NCR53C406A_DEBUG
  80. #define VDEB(x) x
  81. #else
  82. #define VDEB(x)
  83. #endif
  84. #define LOAD_DMA_COUNT(count) \
  85. outb(count & 0xff, TC_LSB); \
  86. outb((count >> 8) & 0xff, TC_MSB); \
  87. outb((count >> 16) & 0xff, TC_HIGH);
  88. /* Chip commands */
  89. #define DMA_OP 0x80
  90. #define SCSI_NOP 0x00
  91. #define FLUSH_FIFO 0x01
  92. #define CHIP_RESET 0x02
  93. #define SCSI_RESET 0x03
  94. #define RESELECT 0x40
  95. #define SELECT_NO_ATN 0x41
  96. #define SELECT_ATN 0x42
  97. #define SELECT_ATN_STOP 0x43
  98. #define ENABLE_SEL 0x44
  99. #define DISABLE_SEL 0x45
  100. #define SELECT_ATN3 0x46
  101. #define RESELECT3 0x47
  102. #define TRANSFER_INFO 0x10
  103. #define INIT_CMD_COMPLETE 0x11
  104. #define MSG_ACCEPT 0x12
  105. #define TRANSFER_PAD 0x18
  106. #define SET_ATN 0x1a
  107. #define RESET_ATN 0x1b
  108. #define SEND_MSG 0x20
  109. #define SEND_STATUS 0x21
  110. #define SEND_DATA 0x22
  111. #define DISCONN_SEQ 0x23
  112. #define TERMINATE_SEQ 0x24
  113. #define TARG_CMD_COMPLETE 0x25
  114. #define DISCONN 0x27
  115. #define RECV_MSG 0x28
  116. #define RECV_CMD 0x29
  117. #define RECV_DATA 0x2a
  118. #define RECV_CMD_SEQ 0x2b
  119. #define TARGET_ABORT_DMA 0x04
  120. /*----------------------------------------------------------------*/
  121. /* the following will set the monitor border color (useful to find
  122. where something crashed or gets stuck at */
  123. /* 1 = blue
  124. 2 = green
  125. 3 = cyan
  126. 4 = red
  127. 5 = magenta
  128. 6 = yellow
  129. 7 = white
  130. */
  131. #if NCR53C406A_DEBUG
  132. #define rtrc(i) {inb(0x3da);outb(0x31,0x3c0);outb((i),0x3c0);}
  133. #else
  134. #define rtrc(i) {}
  135. #endif
  136. /*----------------------------------------------------------------*/
  137. enum Phase {
  138. idle,
  139. data_out,
  140. data_in,
  141. command_ph,
  142. status_ph,
  143. message_out,
  144. message_in
  145. };
  146. /* Static function prototypes */
  147. static void NCR53c406a_intr(void *);
  148. static irqreturn_t do_NCR53c406a_intr(int, void *);
  149. static void chip_init(void);
  150. static void calc_port_addr(void);
  151. #ifndef IRQ_LEV
  152. static int irq_probe(void);
  153. #endif
  154. /* ================================================================= */
  155. #if USE_BIOS
  156. static void *bios_base;
  157. #endif
  158. #ifdef PORT_BASE
  159. static int port_base = PORT_BASE;
  160. #else
  161. static int port_base;
  162. #endif
  163. #ifdef IRQ_LEV
  164. static int irq_level = IRQ_LEV;
  165. #else
  166. static int irq_level = -1; /* 0 is 'no irq', so use -1 for 'uninitialized' */
  167. #endif
  168. #if USE_DMA
  169. static int dma_chan;
  170. #endif
  171. #if USE_PIO
  172. static int fast_pio = USE_FAST_PIO;
  173. #endif
  174. static Scsi_Cmnd *current_SC;
  175. static char info_msg[256];
  176. /* ================================================================= */
  177. /* possible BIOS locations */
  178. #if USE_BIOS
  179. static void *addresses[] = {
  180. (void *) 0xd8000,
  181. (void *) 0xc8000
  182. };
  183. #define ADDRESS_COUNT ARRAY_SIZE(addresses)
  184. #endif /* USE_BIOS */
  185. /* possible i/o port addresses */
  186. static unsigned short ports[] = { 0x230, 0x330, 0x280, 0x290, 0x330, 0x340, 0x300, 0x310, 0x348, 0x350 };
  187. #define PORT_COUNT ARRAY_SIZE(ports)
  188. #ifndef MODULE
  189. /* possible interrupt channels */
  190. static unsigned short intrs[] = { 10, 11, 12, 15 };
  191. #define INTR_COUNT ARRAY_SIZE(intrs)
  192. #endif /* !MODULE */
  193. /* signatures for NCR 53c406a based controllers */
  194. #if USE_BIOS
  195. struct signature {
  196. char *signature;
  197. int sig_offset;
  198. int sig_length;
  199. } signatures[] __initdata = {
  200. /* 1 2 3 4 5 6 */
  201. /* 123456789012345678901234567890123456789012345678901234567890 */
  202. {
  203. "Copyright (C) Acculogic, Inc.\r\n2.8M Diskette Extension Bios ver 4.04.03 03/01/1993", 61, 82},};
  204. #define SIGNATURE_COUNT ARRAY_SIZE(signatures)
  205. #endif /* USE_BIOS */
  206. /* ============================================================ */
  207. /* Control Register Set 0 */
  208. static int TC_LSB; /* transfer counter lsb */
  209. static int TC_MSB; /* transfer counter msb */
  210. static int SCSI_FIFO; /* scsi fifo register */
  211. static int CMD_REG; /* command register */
  212. static int STAT_REG; /* status register */
  213. static int DEST_ID; /* selection/reselection bus id */
  214. static int INT_REG; /* interrupt status register */
  215. static int SRTIMOUT; /* select/reselect timeout reg */
  216. static int SEQ_REG; /* sequence step register */
  217. static int SYNCPRD; /* synchronous transfer period */
  218. static int FIFO_FLAGS; /* indicates # of bytes in fifo */
  219. static int SYNCOFF; /* synchronous offset register */
  220. static int CONFIG1; /* configuration register */
  221. static int CLKCONV; /* clock conversion reg */
  222. /*static int TESTREG;*//* test mode register */
  223. static int CONFIG2; /* Configuration 2 Register */
  224. static int CONFIG3; /* Configuration 3 Register */
  225. static int CONFIG4; /* Configuration 4 Register */
  226. static int TC_HIGH; /* Transfer Counter High */
  227. /*static int FIFO_BOTTOM;*//* Reserve FIFO byte register */
  228. /* Control Register Set 1 */
  229. /*static int JUMPER_SENSE;*//* Jumper sense port reg (r/w) */
  230. /*static int SRAM_PTR;*//* SRAM address pointer reg (r/w) */
  231. /*static int SRAM_DATA;*//* SRAM data register (r/w) */
  232. static int PIO_FIFO; /* PIO FIFO registers (r/w) */
  233. /*static int PIO_FIFO1;*//* */
  234. /*static int PIO_FIFO2;*//* */
  235. /*static int PIO_FIFO3;*//* */
  236. static int PIO_STATUS; /* PIO status (r/w) */
  237. /*static int ATA_CMD;*//* ATA command/status reg (r/w) */
  238. /*static int ATA_ERR;*//* ATA features/error register (r/w) */
  239. static int PIO_FLAG; /* PIO flag interrupt enable (r/w) */
  240. static int CONFIG5; /* Configuration 5 register (r/w) */
  241. /*static int SIGNATURE;*//* Signature Register (r) */
  242. /*static int CONFIG6;*//* Configuration 6 register (r) */
  243. /* ============================================================== */
  244. #if USE_DMA
  245. static __inline__ int NCR53c406a_dma_setup(unsigned char *ptr, unsigned int count, unsigned char mode)
  246. {
  247. unsigned limit;
  248. unsigned long flags = 0;
  249. VDEB(printk("dma: before count=%d ", count));
  250. if (dma_chan <= 3) {
  251. if (count > 65536)
  252. count = 65536;
  253. limit = 65536 - (((unsigned) ptr) & 0xFFFF);
  254. } else {
  255. if (count > (65536 << 1))
  256. count = (65536 << 1);
  257. limit = (65536 << 1) - (((unsigned) ptr) & 0x1FFFF);
  258. }
  259. if (count > limit)
  260. count = limit;
  261. VDEB(printk("after count=%d\n", count));
  262. if ((count & 1) || (((unsigned) ptr) & 1))
  263. panic("NCR53c406a: attempted unaligned DMA transfer\n");
  264. flags = claim_dma_lock();
  265. disable_dma(dma_chan);
  266. clear_dma_ff(dma_chan);
  267. set_dma_addr(dma_chan, (long) ptr);
  268. set_dma_count(dma_chan, count);
  269. set_dma_mode(dma_chan, mode);
  270. enable_dma(dma_chan);
  271. release_dma_lock(flags);
  272. return count;
  273. }
  274. static __inline__ int NCR53c406a_dma_write(unsigned char *src, unsigned int count)
  275. {
  276. return NCR53c406a_dma_setup(src, count, DMA_MODE_WRITE);
  277. }
  278. static __inline__ int NCR53c406a_dma_read(unsigned char *src, unsigned int count)
  279. {
  280. return NCR53c406a_dma_setup(src, count, DMA_MODE_READ);
  281. }
  282. static __inline__ int NCR53c406a_dma_residual(void)
  283. {
  284. register int tmp;
  285. unsigned long flags;
  286. flags = claim_dma_lock();
  287. clear_dma_ff(dma_chan);
  288. tmp = get_dma_residue(dma_chan);
  289. release_dma_lock(flags);
  290. return tmp;
  291. }
  292. #endif /* USE_DMA */
  293. #if USE_PIO
  294. static __inline__ int NCR53c406a_pio_read(unsigned char *request, unsigned int reqlen)
  295. {
  296. int i;
  297. int len; /* current scsi fifo size */
  298. REG1;
  299. while (reqlen) {
  300. i = inb(PIO_STATUS);
  301. /* VDEB(printk("pio_status=%x\n", i)); */
  302. if (i & 0x80)
  303. return 0;
  304. switch (i & 0x1e) {
  305. default:
  306. case 0x10:
  307. len = 0;
  308. break;
  309. case 0x0:
  310. len = 1;
  311. break;
  312. case 0x8:
  313. len = 42;
  314. break;
  315. case 0xc:
  316. len = 84;
  317. break;
  318. case 0xe:
  319. len = 128;
  320. break;
  321. }
  322. if ((i & 0x40) && len == 0) { /* fifo empty and interrupt occurred */
  323. return 0;
  324. }
  325. if (len) {
  326. if (len > reqlen)
  327. len = reqlen;
  328. if (fast_pio && len > 3) {
  329. insl(PIO_FIFO, request, len >> 2);
  330. request += len & 0xfc;
  331. reqlen -= len & 0xfc;
  332. } else {
  333. while (len--) {
  334. *request++ = inb(PIO_FIFO);
  335. reqlen--;
  336. }
  337. }
  338. }
  339. }
  340. return 0;
  341. }
  342. static __inline__ int NCR53c406a_pio_write(unsigned char *request, unsigned int reqlen)
  343. {
  344. int i = 0;
  345. int len; /* current scsi fifo size */
  346. REG1;
  347. while (reqlen && !(i & 0x40)) {
  348. i = inb(PIO_STATUS);
  349. /* VDEB(printk("pio_status=%x\n", i)); */
  350. if (i & 0x80) /* error */
  351. return 0;
  352. switch (i & 0x1e) {
  353. case 0x10:
  354. len = 128;
  355. break;
  356. case 0x0:
  357. len = 84;
  358. break;
  359. case 0x8:
  360. len = 42;
  361. break;
  362. case 0xc:
  363. len = 1;
  364. break;
  365. default:
  366. case 0xe:
  367. len = 0;
  368. break;
  369. }
  370. if (len) {
  371. if (len > reqlen)
  372. len = reqlen;
  373. if (fast_pio && len > 3) {
  374. outsl(PIO_FIFO, request, len >> 2);
  375. request += len & 0xfc;
  376. reqlen -= len & 0xfc;
  377. } else {
  378. while (len--) {
  379. outb(*request++, PIO_FIFO);
  380. reqlen--;
  381. }
  382. }
  383. }
  384. }
  385. return 0;
  386. }
  387. #endif /* USE_PIO */
  388. static int __init NCR53c406a_detect(struct scsi_host_template * tpnt)
  389. {
  390. int present = 0;
  391. struct Scsi_Host *shpnt = NULL;
  392. #ifndef PORT_BASE
  393. int i;
  394. #endif
  395. #if USE_BIOS
  396. int ii, jj;
  397. bios_base = 0;
  398. /* look for a valid signature */
  399. for (ii = 0; ii < ADDRESS_COUNT && !bios_base; ii++)
  400. for (jj = 0; (jj < SIGNATURE_COUNT) && !bios_base; jj++)
  401. if (!memcmp((void *) addresses[ii] + signatures[jj].sig_offset, (void *) signatures[jj].signature, (int) signatures[jj].sig_length))
  402. bios_base = addresses[ii];
  403. if (!bios_base) {
  404. printk("NCR53c406a: BIOS signature not found\n");
  405. return 0;
  406. }
  407. DEB(printk("NCR53c406a BIOS found at 0x%x\n", (unsigned int) bios_base);
  408. );
  409. #endif /* USE_BIOS */
  410. #ifdef PORT_BASE
  411. if (!request_region(port_base, 0x10, "NCR53c406a")) /* ports already snatched */
  412. port_base = 0;
  413. #else /* autodetect */
  414. if (port_base) { /* LILO override */
  415. if (!request_region(port_base, 0x10, "NCR53c406a"))
  416. port_base = 0;
  417. } else {
  418. for (i = 0; i < PORT_COUNT && !port_base; i++) {
  419. if (!request_region(ports[i], 0x10, "NCR53c406a")) {
  420. DEB(printk("NCR53c406a: port 0x%x in use\n", ports[i]));
  421. } else {
  422. VDEB(printk("NCR53c406a: port 0x%x available\n", ports[i]));
  423. outb(C5_IMG, ports[i] + 0x0d); /* reg set 1 */
  424. if ((inb(ports[i] + 0x0e) ^ inb(ports[i] + 0x0e)) == 7 && (inb(ports[i] + 0x0e) ^ inb(ports[i] + 0x0e)) == 7 && (inb(ports[i] + 0x0e) & 0xf8) == 0x58) {
  425. port_base = ports[i];
  426. VDEB(printk("NCR53c406a: Sig register valid\n"));
  427. VDEB(printk("port_base=0x%x\n", port_base));
  428. break;
  429. }
  430. release_region(ports[i], 0x10);
  431. }
  432. }
  433. }
  434. #endif /* PORT_BASE */
  435. if (!port_base) { /* no ports found */
  436. printk("NCR53c406a: no available ports found\n");
  437. return 0;
  438. }
  439. DEB(printk("NCR53c406a detected\n"));
  440. calc_port_addr();
  441. chip_init();
  442. #ifndef IRQ_LEV
  443. if (irq_level < 0) { /* LILO override if >= 0 */
  444. irq_level = irq_probe();
  445. if (irq_level < 0) { /* Trouble */
  446. printk("NCR53c406a: IRQ problem, irq_level=%d, giving up\n", irq_level);
  447. goto err_release;
  448. }
  449. }
  450. #endif
  451. DEB(printk("NCR53c406a: using port_base 0x%x\n", port_base));
  452. present = 1;
  453. tpnt->proc_name = "NCR53c406a";
  454. shpnt = scsi_register(tpnt, 0);
  455. if (!shpnt) {
  456. printk("NCR53c406a: Unable to register host, giving up.\n");
  457. goto err_release;
  458. }
  459. if (irq_level > 0) {
  460. if (request_irq(irq_level, do_NCR53c406a_intr, 0, "NCR53c406a", shpnt)) {
  461. printk("NCR53c406a: unable to allocate IRQ %d\n", irq_level);
  462. goto err_free_scsi;
  463. }
  464. tpnt->can_queue = 1;
  465. DEB(printk("NCR53c406a: allocated IRQ %d\n", irq_level));
  466. } else if (irq_level == 0) {
  467. tpnt->can_queue = 0;
  468. DEB(printk("NCR53c406a: No interrupts detected\n"));
  469. printk("NCR53c406a driver no longer supports polling interface\n");
  470. printk("Please email linux-scsi@vger.kernel.org\n");
  471. #if USE_DMA
  472. printk("NCR53c406a: No interrupts found and DMA mode defined. Giving up.\n");
  473. #endif /* USE_DMA */
  474. goto err_free_scsi;
  475. } else {
  476. DEB(printk("NCR53c406a: Shouldn't get here!\n"));
  477. goto err_free_scsi;
  478. }
  479. #if USE_DMA
  480. dma_chan = DMA_CHAN;
  481. if (request_dma(dma_chan, "NCR53c406a") != 0) {
  482. printk("NCR53c406a: unable to allocate DMA channel %d\n", dma_chan);
  483. goto err_free_irq;
  484. }
  485. DEB(printk("Allocated DMA channel %d\n", dma_chan));
  486. #endif /* USE_DMA */
  487. shpnt->irq = irq_level;
  488. shpnt->io_port = port_base;
  489. shpnt->n_io_port = 0x10;
  490. #if USE_DMA
  491. shpnt->dma = dma_chan;
  492. #endif
  493. #if USE_DMA
  494. sprintf(info_msg, "NCR53c406a at 0x%x, IRQ %d, DMA channel %d.", port_base, irq_level, dma_chan);
  495. #else
  496. sprintf(info_msg, "NCR53c406a at 0x%x, IRQ %d, %s PIO mode.", port_base, irq_level, fast_pio ? "fast" : "slow");
  497. #endif
  498. return (present);
  499. #if USE_DMA
  500. err_free_irq:
  501. if (irq_level)
  502. free_irq(irq_level, shpnt);
  503. #endif
  504. err_free_scsi:
  505. scsi_unregister(shpnt);
  506. err_release:
  507. release_region(port_base, 0x10);
  508. return 0;
  509. }
  510. static int NCR53c406a_release(struct Scsi_Host *shost)
  511. {
  512. if (shost->irq)
  513. free_irq(shost->irq, NULL);
  514. #ifdef USE_DMA
  515. if (shost->dma_channel != 0xff)
  516. free_dma(shost->dma_channel);
  517. #endif
  518. if (shost->io_port && shost->n_io_port)
  519. release_region(shost->io_port, shost->n_io_port);
  520. scsi_unregister(shost);
  521. return 0;
  522. }
  523. #ifndef MODULE
  524. /* called from init/main.c */
  525. static int __init NCR53c406a_setup(char *str)
  526. {
  527. static size_t setup_idx = 0;
  528. size_t i;
  529. int ints[4];
  530. DEB(printk("NCR53c406a: Setup called\n");
  531. );
  532. if (setup_idx >= PORT_COUNT - 1) {
  533. printk("NCR53c406a: Setup called too many times. Bad LILO params?\n");
  534. return 0;
  535. }
  536. get_options(str, 4, ints);
  537. if (ints[0] < 1 || ints[0] > 3) {
  538. printk("NCR53c406a: Malformed command line\n");
  539. printk("NCR53c406a: Usage: ncr53c406a=<PORTBASE>[,<IRQ>[,<FASTPIO>]]\n");
  540. return 0;
  541. }
  542. for (i = 0; i < PORT_COUNT && !port_base; i++)
  543. if (ports[i] == ints[1]) {
  544. port_base = ints[1];
  545. DEB(printk("NCR53c406a: Specified port_base 0x%x\n", port_base);
  546. )
  547. }
  548. if (!port_base) {
  549. printk("NCR53c406a: Invalid PORTBASE 0x%x specified\n", ints[1]);
  550. return 0;
  551. }
  552. if (ints[0] > 1) {
  553. if (ints[2] == 0) {
  554. irq_level = 0;
  555. DEB(printk("NCR53c406a: Specified irq %d\n", irq_level);
  556. )
  557. } else
  558. for (i = 0; i < INTR_COUNT && irq_level < 0; i++)
  559. if (intrs[i] == ints[2]) {
  560. irq_level = ints[2];
  561. DEB(printk("NCR53c406a: Specified irq %d\n", port_base);
  562. )
  563. }
  564. if (irq_level < 0)
  565. printk("NCR53c406a: Invalid IRQ %d specified\n", ints[2]);
  566. }
  567. if (ints[0] > 2)
  568. fast_pio = ints[3];
  569. DEB(printk("NCR53c406a: port_base=0x%x, irq=%d, fast_pio=%d\n", port_base, irq_level, fast_pio);)
  570. return 1;
  571. }
  572. __setup("ncr53c406a=", NCR53c406a_setup);
  573. #endif /* !MODULE */
  574. static const char *NCR53c406a_info(struct Scsi_Host *SChost)
  575. {
  576. DEB(printk("NCR53c406a_info called\n"));
  577. return (info_msg);
  578. }
  579. #if 0
  580. static void wait_intr(void)
  581. {
  582. unsigned long i = jiffies + WATCHDOG;
  583. while (time_after(i, jiffies) && !(inb(STAT_REG) & 0xe0)) { /* wait for a pseudo-interrupt */
  584. cpu_relax();
  585. barrier();
  586. }
  587. if (time_before_eq(i, jiffies)) { /* Timed out */
  588. rtrc(0);
  589. current_SC->result = DID_TIME_OUT << 16;
  590. current_SC->SCp.phase = idle;
  591. current_SC->scsi_done(current_SC);
  592. return;
  593. }
  594. NCR53c406a_intr(NULL);
  595. }
  596. #endif
  597. static int NCR53c406a_queue(Scsi_Cmnd * SCpnt, void (*done) (Scsi_Cmnd *))
  598. {
  599. int i;
  600. VDEB(printk("NCR53c406a_queue called\n"));
  601. DEB(printk("cmd=%02x, cmd_len=%02x, target=%02x, lun=%02x, bufflen=%d\n", SCpnt->cmnd[0], SCpnt->cmd_len, SCpnt->target, SCpnt->lun, SCpnt->request_bufflen));
  602. #if 0
  603. VDEB(for (i = 0; i < SCpnt->cmd_len; i++)
  604. printk("cmd[%d]=%02x ", i, SCpnt->cmnd[i]));
  605. VDEB(printk("\n"));
  606. #endif
  607. current_SC = SCpnt;
  608. current_SC->scsi_done = done;
  609. current_SC->SCp.phase = command_ph;
  610. current_SC->SCp.Status = 0;
  611. current_SC->SCp.Message = 0;
  612. /* We are locked here already by the mid layer */
  613. REG0;
  614. outb(scmd_id(SCpnt), DEST_ID); /* set destination */
  615. outb(FLUSH_FIFO, CMD_REG); /* reset the fifos */
  616. for (i = 0; i < SCpnt->cmd_len; i++) {
  617. outb(SCpnt->cmnd[i], SCSI_FIFO);
  618. }
  619. outb(SELECT_NO_ATN, CMD_REG);
  620. rtrc(1);
  621. return 0;
  622. }
  623. static int NCR53c406a_host_reset(Scsi_Cmnd * SCpnt)
  624. {
  625. DEB(printk("NCR53c406a_reset called\n"));
  626. spin_lock_irq(SCpnt->device->host->host_lock);
  627. outb(C4_IMG, CONFIG4); /* Select reg set 0 */
  628. outb(CHIP_RESET, CMD_REG);
  629. outb(SCSI_NOP, CMD_REG); /* required after reset */
  630. outb(SCSI_RESET, CMD_REG);
  631. chip_init();
  632. rtrc(2);
  633. spin_unlock_irq(SCpnt->device->host->host_lock);
  634. return SUCCESS;
  635. }
  636. static int NCR53c406a_biosparm(struct scsi_device *disk,
  637. struct block_device *dev,
  638. sector_t capacity, int *info_array)
  639. {
  640. int size;
  641. DEB(printk("NCR53c406a_biosparm called\n"));
  642. size = capacity;
  643. info_array[0] = 64; /* heads */
  644. info_array[1] = 32; /* sectors */
  645. info_array[2] = size >> 11; /* cylinders */
  646. if (info_array[2] > 1024) { /* big disk */
  647. info_array[0] = 255;
  648. info_array[1] = 63;
  649. info_array[2] = size / (255 * 63);
  650. }
  651. return 0;
  652. }
  653. static irqreturn_t do_NCR53c406a_intr(int unused, void *dev_id)
  654. {
  655. unsigned long flags;
  656. struct Scsi_Host *dev = dev_id;
  657. spin_lock_irqsave(dev->host_lock, flags);
  658. NCR53c406a_intr(dev_id);
  659. spin_unlock_irqrestore(dev->host_lock, flags);
  660. return IRQ_HANDLED;
  661. }
  662. static void NCR53c406a_intr(void *dev_id)
  663. {
  664. DEB(unsigned char fifo_size;
  665. )
  666. DEB(unsigned char seq_reg;
  667. )
  668. unsigned char status, int_reg;
  669. #if USE_PIO
  670. unsigned char pio_status;
  671. struct scatterlist *sglist;
  672. unsigned int sgcount;
  673. #endif
  674. VDEB(printk("NCR53c406a_intr called\n"));
  675. #if USE_PIO
  676. REG1;
  677. pio_status = inb(PIO_STATUS);
  678. #endif
  679. REG0;
  680. status = inb(STAT_REG);
  681. DEB(seq_reg = inb(SEQ_REG));
  682. int_reg = inb(INT_REG);
  683. DEB(fifo_size = inb(FIFO_FLAGS) & 0x1f);
  684. #if NCR53C406A_DEBUG
  685. printk("status=%02x, seq_reg=%02x, int_reg=%02x, fifo_size=%02x", status, seq_reg, int_reg, fifo_size);
  686. #if (USE_DMA)
  687. printk("\n");
  688. #else
  689. printk(", pio=%02x\n", pio_status);
  690. #endif /* USE_DMA */
  691. #endif /* NCR53C406A_DEBUG */
  692. if (int_reg & 0x80) { /* SCSI reset intr */
  693. rtrc(3);
  694. DEB(printk("NCR53c406a: reset intr received\n"));
  695. current_SC->SCp.phase = idle;
  696. current_SC->result = DID_RESET << 16;
  697. current_SC->scsi_done(current_SC);
  698. return;
  699. }
  700. #if USE_PIO
  701. if (pio_status & 0x80) {
  702. printk("NCR53C406A: Warning: PIO error!\n");
  703. current_SC->SCp.phase = idle;
  704. current_SC->result = DID_ERROR << 16;
  705. current_SC->scsi_done(current_SC);
  706. return;
  707. }
  708. #endif /* USE_PIO */
  709. if (status & 0x20) { /* Parity error */
  710. printk("NCR53c406a: Warning: parity error!\n");
  711. current_SC->SCp.phase = idle;
  712. current_SC->result = DID_PARITY << 16;
  713. current_SC->scsi_done(current_SC);
  714. return;
  715. }
  716. if (status & 0x40) { /* Gross error */
  717. printk("NCR53c406a: Warning: gross error!\n");
  718. current_SC->SCp.phase = idle;
  719. current_SC->result = DID_ERROR << 16;
  720. current_SC->scsi_done(current_SC);
  721. return;
  722. }
  723. if (int_reg & 0x20) { /* Disconnect */
  724. DEB(printk("NCR53c406a: disconnect intr received\n"));
  725. if (current_SC->SCp.phase != message_in) { /* Unexpected disconnect */
  726. current_SC->result = DID_NO_CONNECT << 16;
  727. } else { /* Command complete, return status and message */
  728. current_SC->result = (current_SC->SCp.Status & 0xff)
  729. | ((current_SC->SCp.Message & 0xff) << 8) | (DID_OK << 16);
  730. }
  731. rtrc(0);
  732. current_SC->SCp.phase = idle;
  733. current_SC->scsi_done(current_SC);
  734. return;
  735. }
  736. switch (status & 0x07) { /* scsi phase */
  737. case 0x00: /* DATA-OUT */
  738. if (int_reg & 0x10) { /* Target requesting info transfer */
  739. rtrc(5);
  740. current_SC->SCp.phase = data_out;
  741. VDEB(printk("NCR53c406a: Data-Out phase\n"));
  742. outb(FLUSH_FIFO, CMD_REG);
  743. LOAD_DMA_COUNT(current_SC->request_bufflen); /* Max transfer size */
  744. #if USE_DMA /* No s/g support for DMA */
  745. NCR53c406a_dma_write(current_SC->request_buffer, current_SC->request_bufflen);
  746. #endif /* USE_DMA */
  747. outb(TRANSFER_INFO | DMA_OP, CMD_REG);
  748. #if USE_PIO
  749. if (!current_SC->use_sg) /* Don't use scatter-gather */
  750. NCR53c406a_pio_write(current_SC->request_buffer, current_SC->request_bufflen);
  751. else { /* use scatter-gather */
  752. sgcount = current_SC->use_sg;
  753. sglist = current_SC->request_buffer;
  754. while (sgcount--) {
  755. NCR53c406a_pio_write(page_address(sglist->page) + sglist->offset, sglist->length);
  756. sglist++;
  757. }
  758. }
  759. REG0;
  760. #endif /* USE_PIO */
  761. }
  762. break;
  763. case 0x01: /* DATA-IN */
  764. if (int_reg & 0x10) { /* Target requesting info transfer */
  765. rtrc(6);
  766. current_SC->SCp.phase = data_in;
  767. VDEB(printk("NCR53c406a: Data-In phase\n"));
  768. outb(FLUSH_FIFO, CMD_REG);
  769. LOAD_DMA_COUNT(current_SC->request_bufflen); /* Max transfer size */
  770. #if USE_DMA /* No s/g support for DMA */
  771. NCR53c406a_dma_read(current_SC->request_buffer, current_SC->request_bufflen);
  772. #endif /* USE_DMA */
  773. outb(TRANSFER_INFO | DMA_OP, CMD_REG);
  774. #if USE_PIO
  775. if (!current_SC->use_sg) /* Don't use scatter-gather */
  776. NCR53c406a_pio_read(current_SC->request_buffer, current_SC->request_bufflen);
  777. else { /* Use scatter-gather */
  778. sgcount = current_SC->use_sg;
  779. sglist = current_SC->request_buffer;
  780. while (sgcount--) {
  781. NCR53c406a_pio_read(page_address(sglist->page) + sglist->offset, sglist->length);
  782. sglist++;
  783. }
  784. }
  785. REG0;
  786. #endif /* USE_PIO */
  787. }
  788. break;
  789. case 0x02: /* COMMAND */
  790. current_SC->SCp.phase = command_ph;
  791. printk("NCR53c406a: Warning: Unknown interrupt occurred in command phase!\n");
  792. break;
  793. case 0x03: /* STATUS */
  794. rtrc(7);
  795. current_SC->SCp.phase = status_ph;
  796. VDEB(printk("NCR53c406a: Status phase\n"));
  797. outb(FLUSH_FIFO, CMD_REG);
  798. outb(INIT_CMD_COMPLETE, CMD_REG);
  799. break;
  800. case 0x04: /* Reserved */
  801. case 0x05: /* Reserved */
  802. printk("NCR53c406a: WARNING: Reserved phase!!!\n");
  803. break;
  804. case 0x06: /* MESSAGE-OUT */
  805. DEB(printk("NCR53c406a: Message-Out phase\n"));
  806. current_SC->SCp.phase = message_out;
  807. outb(SET_ATN, CMD_REG); /* Reject the message */
  808. outb(MSG_ACCEPT, CMD_REG);
  809. break;
  810. case 0x07: /* MESSAGE-IN */
  811. rtrc(4);
  812. VDEB(printk("NCR53c406a: Message-In phase\n"));
  813. current_SC->SCp.phase = message_in;
  814. current_SC->SCp.Status = inb(SCSI_FIFO);
  815. current_SC->SCp.Message = inb(SCSI_FIFO);
  816. VDEB(printk("SCSI FIFO size=%d\n", inb(FIFO_FLAGS) & 0x1f));
  817. DEB(printk("Status = %02x Message = %02x\n", current_SC->SCp.Status, current_SC->SCp.Message));
  818. if (current_SC->SCp.Message == SAVE_POINTERS || current_SC->SCp.Message == DISCONNECT) {
  819. outb(SET_ATN, CMD_REG); /* Reject message */
  820. DEB(printk("Discarding SAVE_POINTERS message\n"));
  821. }
  822. outb(MSG_ACCEPT, CMD_REG);
  823. break;
  824. }
  825. }
  826. #ifndef IRQ_LEV
  827. static int irq_probe(void)
  828. {
  829. int irqs, irq;
  830. unsigned long i;
  831. inb(INT_REG); /* clear the interrupt register */
  832. irqs = probe_irq_on();
  833. /* Invalid command will cause an interrupt */
  834. REG0;
  835. outb(0xff, CMD_REG);
  836. /* Wait for the interrupt to occur */
  837. i = jiffies + WATCHDOG;
  838. while (time_after(i, jiffies) && !(inb(STAT_REG) & 0x80))
  839. barrier();
  840. if (time_before_eq(i, jiffies)) { /* Timed out, must be hardware trouble */
  841. probe_irq_off(irqs);
  842. return -1;
  843. }
  844. irq = probe_irq_off(irqs);
  845. /* Kick the chip */
  846. outb(CHIP_RESET, CMD_REG);
  847. outb(SCSI_NOP, CMD_REG);
  848. chip_init();
  849. return irq;
  850. }
  851. #endif /* IRQ_LEV */
  852. static void chip_init(void)
  853. {
  854. REG1;
  855. #if USE_DMA
  856. outb(0x00, PIO_STATUS);
  857. #else /* USE_PIO */
  858. outb(0x01, PIO_STATUS);
  859. #endif
  860. outb(0x00, PIO_FLAG);
  861. outb(C4_IMG, CONFIG4); /* REG0; */
  862. outb(C3_IMG, CONFIG3);
  863. outb(C2_IMG, CONFIG2);
  864. outb(C1_IMG, CONFIG1);
  865. outb(0x05, CLKCONV); /* clock conversion factor */
  866. outb(0x9C, SRTIMOUT); /* Selection timeout */
  867. outb(0x05, SYNCPRD); /* Synchronous transfer period */
  868. outb(SYNC_MODE, SYNCOFF); /* synchronous mode */
  869. }
  870. static void __init calc_port_addr(void)
  871. {
  872. /* Control Register Set 0 */
  873. TC_LSB = (port_base + 0x00);
  874. TC_MSB = (port_base + 0x01);
  875. SCSI_FIFO = (port_base + 0x02);
  876. CMD_REG = (port_base + 0x03);
  877. STAT_REG = (port_base + 0x04);
  878. DEST_ID = (port_base + 0x04);
  879. INT_REG = (port_base + 0x05);
  880. SRTIMOUT = (port_base + 0x05);
  881. SEQ_REG = (port_base + 0x06);
  882. SYNCPRD = (port_base + 0x06);
  883. FIFO_FLAGS = (port_base + 0x07);
  884. SYNCOFF = (port_base + 0x07);
  885. CONFIG1 = (port_base + 0x08);
  886. CLKCONV = (port_base + 0x09);
  887. /* TESTREG = (port_base+0x0A); */
  888. CONFIG2 = (port_base + 0x0B);
  889. CONFIG3 = (port_base + 0x0C);
  890. CONFIG4 = (port_base + 0x0D);
  891. TC_HIGH = (port_base + 0x0E);
  892. /* FIFO_BOTTOM = (port_base+0x0F); */
  893. /* Control Register Set 1 */
  894. /* JUMPER_SENSE = (port_base+0x00); */
  895. /* SRAM_PTR = (port_base+0x01); */
  896. /* SRAM_DATA = (port_base+0x02); */
  897. PIO_FIFO = (port_base + 0x04);
  898. /* PIO_FIFO1 = (port_base+0x05); */
  899. /* PIO_FIFO2 = (port_base+0x06); */
  900. /* PIO_FIFO3 = (port_base+0x07); */
  901. PIO_STATUS = (port_base + 0x08);
  902. /* ATA_CMD = (port_base+0x09); */
  903. /* ATA_ERR = (port_base+0x0A); */
  904. PIO_FLAG = (port_base + 0x0B);
  905. CONFIG5 = (port_base + 0x0D);
  906. /* SIGNATURE = (port_base+0x0E); */
  907. /* CONFIG6 = (port_base+0x0F); */
  908. }
  909. MODULE_LICENSE("GPL");
  910. /* NOTE: scatter-gather support only works in PIO mode.
  911. * Use SG_NONE if DMA mode is enabled!
  912. */
  913. static struct scsi_host_template driver_template =
  914. {
  915. .proc_name = "NCR53c406a" /* proc_name */,
  916. .name = "NCR53c406a" /* name */,
  917. .detect = NCR53c406a_detect /* detect */,
  918. .release = NCR53c406a_release,
  919. .info = NCR53c406a_info /* info */,
  920. .queuecommand = NCR53c406a_queue /* queuecommand */,
  921. .eh_host_reset_handler = NCR53c406a_host_reset /* reset */,
  922. .bios_param = NCR53c406a_biosparm /* biosparm */,
  923. .can_queue = 1 /* can_queue */,
  924. .this_id = 7 /* SCSI ID of the chip */,
  925. .sg_tablesize = 32 /*SG_ALL*/ /*SG_NONE*/,
  926. .cmd_per_lun = 1 /* commands per lun */,
  927. .unchecked_isa_dma = 1 /* unchecked_isa_dma */,
  928. .use_clustering = ENABLE_CLUSTERING
  929. };
  930. #include "scsi_module.c"
  931. /*
  932. * Overrides for Emacs so that we get a uniform tabbing style.
  933. * Emacs will notice this stuff at the end of the file and automatically
  934. * adjust the settings for this buffer only. This must remain at the end
  935. * of the file.
  936. * ---------------------------------------------------------------------------
  937. * Local variables:
  938. * c-indent-level: 4
  939. * c-brace-imaginary-offset: 0
  940. * c-brace-offset: -4
  941. * c-argdecl-indent: 4
  942. * c-label-offset: -4
  943. * c-continued-statement-offset: 4
  944. * c-continued-brace-offset: 0
  945. * indent-tabs-mode: nil
  946. * tab-width: 8
  947. * End:
  948. */