NCR53c406a.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110
  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. #if 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(int, void *, struct pt_regs *);
  148. static irqreturn_t do_NCR53c406a_intr(int, void *, struct pt_regs *);
  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. #if PORT_BASE
  159. static int port_base = PORT_BASE;
  160. #else
  161. static int port_base;
  162. #endif
  163. #if 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 (sizeof( addresses ) / sizeof( unsigned ))
  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 (sizeof( ports ) / sizeof( unsigned short ))
  188. /* possible interrupt channels */
  189. static unsigned short intrs[] = { 10, 11, 12, 15 };
  190. #define INTR_COUNT (sizeof( intrs ) / sizeof( unsigned short ))
  191. /* signatures for NCR 53c406a based controllers */
  192. #if USE_BIOS
  193. struct signature {
  194. char *signature;
  195. int sig_offset;
  196. int sig_length;
  197. } signatures[] __initdata = {
  198. /* 1 2 3 4 5 6 */
  199. /* 123456789012345678901234567890123456789012345678901234567890 */
  200. {
  201. "Copyright (C) Acculogic, Inc.\r\n2.8M Diskette Extension Bios ver 4.04.03 03/01/1993", 61, 82},};
  202. #define SIGNATURE_COUNT (sizeof( signatures ) / sizeof( struct signature ))
  203. #endif /* USE_BIOS */
  204. /* ============================================================ */
  205. /* Control Register Set 0 */
  206. static int TC_LSB; /* transfer counter lsb */
  207. static int TC_MSB; /* transfer counter msb */
  208. static int SCSI_FIFO; /* scsi fifo register */
  209. static int CMD_REG; /* command register */
  210. static int STAT_REG; /* status register */
  211. static int DEST_ID; /* selection/reselection bus id */
  212. static int INT_REG; /* interrupt status register */
  213. static int SRTIMOUT; /* select/reselect timeout reg */
  214. static int SEQ_REG; /* sequence step register */
  215. static int SYNCPRD; /* synchronous transfer period */
  216. static int FIFO_FLAGS; /* indicates # of bytes in fifo */
  217. static int SYNCOFF; /* synchronous offset register */
  218. static int CONFIG1; /* configuration register */
  219. static int CLKCONV; /* clock conversion reg */
  220. /*static int TESTREG;*//* test mode register */
  221. static int CONFIG2; /* Configuration 2 Register */
  222. static int CONFIG3; /* Configuration 3 Register */
  223. static int CONFIG4; /* Configuration 4 Register */
  224. static int TC_HIGH; /* Transfer Counter High */
  225. /*static int FIFO_BOTTOM;*//* Reserve FIFO byte register */
  226. /* Control Register Set 1 */
  227. /*static int JUMPER_SENSE;*//* Jumper sense port reg (r/w) */
  228. /*static int SRAM_PTR;*//* SRAM address pointer reg (r/w) */
  229. /*static int SRAM_DATA;*//* SRAM data register (r/w) */
  230. static int PIO_FIFO; /* PIO FIFO registers (r/w) */
  231. /*static int PIO_FIFO1;*//* */
  232. /*static int PIO_FIFO2;*//* */
  233. /*static int PIO_FIFO3;*//* */
  234. static int PIO_STATUS; /* PIO status (r/w) */
  235. /*static int ATA_CMD;*//* ATA command/status reg (r/w) */
  236. /*static int ATA_ERR;*//* ATA features/error register (r/w) */
  237. static int PIO_FLAG; /* PIO flag interrupt enable (r/w) */
  238. static int CONFIG5; /* Configuration 5 register (r/w) */
  239. /*static int SIGNATURE;*//* Signature Register (r) */
  240. /*static int CONFIG6;*//* Configuration 6 register (r) */
  241. /* ============================================================== */
  242. #if USE_DMA
  243. static __inline__ int NCR53c406a_dma_setup(unsigned char *ptr, unsigned int count, unsigned char mode)
  244. {
  245. unsigned limit;
  246. unsigned long flags = 0;
  247. VDEB(printk("dma: before count=%d ", count));
  248. if (dma_chan <= 3) {
  249. if (count > 65536)
  250. count = 65536;
  251. limit = 65536 - (((unsigned) ptr) & 0xFFFF);
  252. } else {
  253. if (count > (65536 << 1))
  254. count = (65536 << 1);
  255. limit = (65536 << 1) - (((unsigned) ptr) & 0x1FFFF);
  256. }
  257. if (count > limit)
  258. count = limit;
  259. VDEB(printk("after count=%d\n", count));
  260. if ((count & 1) || (((unsigned) ptr) & 1))
  261. panic("NCR53c406a: attempted unaligned DMA transfer\n");
  262. flags = claim_dma_lock();
  263. disable_dma(dma_chan);
  264. clear_dma_ff(dma_chan);
  265. set_dma_addr(dma_chan, (long) ptr);
  266. set_dma_count(dma_chan, count);
  267. set_dma_mode(dma_chan, mode);
  268. enable_dma(dma_chan);
  269. release_dma_lock(flags);
  270. return count;
  271. }
  272. static __inline__ int NCR53c406a_dma_write(unsigned char *src, unsigned int count)
  273. {
  274. return NCR53c406a_dma_setup(src, count, DMA_MODE_WRITE);
  275. }
  276. static __inline__ int NCR53c406a_dma_read(unsigned char *src, unsigned int count)
  277. {
  278. return NCR53c406a_dma_setup(src, count, DMA_MODE_READ);
  279. }
  280. static __inline__ int NCR53c406a_dma_residual(void)
  281. {
  282. register int tmp;
  283. unsigned long flags;
  284. flags = claim_dma_lock();
  285. clear_dma_ff(dma_chan);
  286. tmp = get_dma_residue(dma_chan);
  287. release_dma_lock(flags);
  288. return tmp;
  289. }
  290. #endif /* USE_DMA */
  291. #if USE_PIO
  292. static __inline__ int NCR53c406a_pio_read(unsigned char *request, unsigned int reqlen)
  293. {
  294. int i;
  295. int len; /* current scsi fifo size */
  296. REG1;
  297. while (reqlen) {
  298. i = inb(PIO_STATUS);
  299. /* VDEB(printk("pio_status=%x\n", i)); */
  300. if (i & 0x80)
  301. return 0;
  302. switch (i & 0x1e) {
  303. default:
  304. case 0x10:
  305. len = 0;
  306. break;
  307. case 0x0:
  308. len = 1;
  309. break;
  310. case 0x8:
  311. len = 42;
  312. break;
  313. case 0xc:
  314. len = 84;
  315. break;
  316. case 0xe:
  317. len = 128;
  318. break;
  319. }
  320. if ((i & 0x40) && len == 0) { /* fifo empty and interrupt occurred */
  321. return 0;
  322. }
  323. if (len) {
  324. if (len > reqlen)
  325. len = reqlen;
  326. if (fast_pio && len > 3) {
  327. insl(PIO_FIFO, request, len >> 2);
  328. request += len & 0xfc;
  329. reqlen -= len & 0xfc;
  330. } else {
  331. while (len--) {
  332. *request++ = inb(PIO_FIFO);
  333. reqlen--;
  334. }
  335. }
  336. }
  337. }
  338. return 0;
  339. }
  340. static __inline__ int NCR53c406a_pio_write(unsigned char *request, unsigned int reqlen)
  341. {
  342. int i = 0;
  343. int len; /* current scsi fifo size */
  344. REG1;
  345. while (reqlen && !(i & 0x40)) {
  346. i = inb(PIO_STATUS);
  347. /* VDEB(printk("pio_status=%x\n", i)); */
  348. if (i & 0x80) /* error */
  349. return 0;
  350. switch (i & 0x1e) {
  351. case 0x10:
  352. len = 128;
  353. break;
  354. case 0x0:
  355. len = 84;
  356. break;
  357. case 0x8:
  358. len = 42;
  359. break;
  360. case 0xc:
  361. len = 1;
  362. break;
  363. default:
  364. case 0xe:
  365. len = 0;
  366. break;
  367. }
  368. if (len) {
  369. if (len > reqlen)
  370. len = reqlen;
  371. if (fast_pio && len > 3) {
  372. outsl(PIO_FIFO, request, len >> 2);
  373. request += len & 0xfc;
  374. reqlen -= len & 0xfc;
  375. } else {
  376. while (len--) {
  377. outb(*request++, PIO_FIFO);
  378. reqlen--;
  379. }
  380. }
  381. }
  382. }
  383. return 0;
  384. }
  385. #endif /* USE_PIO */
  386. static int __init NCR53c406a_detect(Scsi_Host_Template * tpnt)
  387. {
  388. int present = 0;
  389. struct Scsi_Host *shpnt = NULL;
  390. #ifndef PORT_BASE
  391. int i;
  392. #endif
  393. #if USE_BIOS
  394. int ii, jj;
  395. bios_base = 0;
  396. /* look for a valid signature */
  397. for (ii = 0; ii < ADDRESS_COUNT && !bios_base; ii++)
  398. for (jj = 0; (jj < SIGNATURE_COUNT) && !bios_base; jj++)
  399. if (!memcmp((void *) addresses[ii] + signatures[jj].sig_offset, (void *) signatures[jj].signature, (int) signatures[jj].sig_length))
  400. bios_base = addresses[ii];
  401. if (!bios_base) {
  402. printk("NCR53c406a: BIOS signature not found\n");
  403. return 0;
  404. }
  405. DEB(printk("NCR53c406a BIOS found at 0x%x\n", (unsigned int) bios_base);
  406. );
  407. #endif /* USE_BIOS */
  408. #ifdef PORT_BASE
  409. if (!request_region(port_base, 0x10, "NCR53c406a")) /* ports already snatched */
  410. port_base = 0;
  411. #else /* autodetect */
  412. if (port_base) { /* LILO override */
  413. if (!request_region(port_base, 0x10, "NCR53c406a"))
  414. port_base = 0;
  415. } else {
  416. for (i = 0; i < PORT_COUNT && !port_base; i++) {
  417. if (!request_region(ports[i], 0x10, "NCR53c406a")) {
  418. DEB(printk("NCR53c406a: port 0x%x in use\n", ports[i]));
  419. } else {
  420. VDEB(printk("NCR53c406a: port 0x%x available\n", ports[i]));
  421. outb(C5_IMG, ports[i] + 0x0d); /* reg set 1 */
  422. 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) {
  423. port_base = ports[i];
  424. VDEB(printk("NCR53c406a: Sig register valid\n"));
  425. VDEB(printk("port_base=0x%x\n", port_base));
  426. break;
  427. }
  428. release_region(ports[i], 0x10);
  429. }
  430. }
  431. }
  432. #endif /* PORT_BASE */
  433. if (!port_base) { /* no ports found */
  434. printk("NCR53c406a: no available ports found\n");
  435. return 0;
  436. }
  437. DEB(printk("NCR53c406a detected\n"));
  438. calc_port_addr();
  439. chip_init();
  440. #ifndef IRQ_LEV
  441. if (irq_level < 0) { /* LILO override if >= 0 */
  442. irq_level = irq_probe();
  443. if (irq_level < 0) { /* Trouble */
  444. printk("NCR53c406a: IRQ problem, irq_level=%d, giving up\n", irq_level);
  445. goto err_release;
  446. }
  447. }
  448. #endif
  449. DEB(printk("NCR53c406a: using port_base 0x%x\n", port_base));
  450. present = 1;
  451. tpnt->proc_name = "NCR53c406a";
  452. shpnt = scsi_register(tpnt, 0);
  453. if (!shpnt) {
  454. printk("NCR53c406a: Unable to register host, giving up.\n");
  455. goto err_release;
  456. }
  457. if (irq_level > 0) {
  458. if (request_irq(irq_level, do_NCR53c406a_intr, 0, "NCR53c406a", shpnt)) {
  459. printk("NCR53c406a: unable to allocate IRQ %d\n", irq_level);
  460. goto err_free_scsi;
  461. }
  462. tpnt->can_queue = 1;
  463. DEB(printk("NCR53c406a: allocated IRQ %d\n", irq_level));
  464. } else if (irq_level == 0) {
  465. tpnt->can_queue = 0;
  466. DEB(printk("NCR53c406a: No interrupts detected\n"));
  467. printk("NCR53c406a driver no longer supports polling interface\n");
  468. printk("Please email linux-scsi@vger.kernel.org\n");
  469. #if USE_DMA
  470. printk("NCR53c406a: No interrupts found and DMA mode defined. Giving up.\n");
  471. #endif /* USE_DMA */
  472. goto err_free_scsi;
  473. } else {
  474. DEB(printk("NCR53c406a: Shouldn't get here!\n"));
  475. goto err_free_scsi;
  476. }
  477. #if USE_DMA
  478. dma_chan = DMA_CHAN;
  479. if (request_dma(dma_chan, "NCR53c406a") != 0) {
  480. printk("NCR53c406a: unable to allocate DMA channel %d\n", dma_chan);
  481. goto err_free_irq;
  482. }
  483. DEB(printk("Allocated DMA channel %d\n", dma_chan));
  484. #endif /* USE_DMA */
  485. shpnt->irq = irq_level;
  486. shpnt->io_port = port_base;
  487. shpnt->n_io_port = 0x10;
  488. #if USE_DMA
  489. shpnt->dma = dma_chan;
  490. #endif
  491. #if USE_DMA
  492. sprintf(info_msg, "NCR53c406a at 0x%x, IRQ %d, DMA channel %d.", port_base, irq_level, dma_chan);
  493. #else
  494. sprintf(info_msg, "NCR53c406a at 0x%x, IRQ %d, %s PIO mode.", port_base, irq_level, fast_pio ? "fast" : "slow");
  495. #endif
  496. return (present);
  497. #if USE_DMA
  498. err_free_irq:
  499. if (irq_level)
  500. free_irq(irq_level, shpnt);
  501. #endif
  502. err_free_scsi:
  503. scsi_unregister(shpnt);
  504. err_release:
  505. release_region(port_base, 0x10);
  506. return 0;
  507. }
  508. static int NCR53c406a_release(struct Scsi_Host *shost)
  509. {
  510. if (shost->irq)
  511. free_irq(shost->irq, NULL);
  512. #ifdef USE_DMA
  513. if (shost->dma_channel != 0xff)
  514. free_dma(shost->dma_channel);
  515. #endif
  516. if (shost->io_port && shost->n_io_port)
  517. release_region(shost->io_port, shost->n_io_port);
  518. scsi_unregister(shost);
  519. return 0;
  520. }
  521. /* called from init/main.c */
  522. static int __init NCR53c406a_setup(char *str)
  523. {
  524. static size_t setup_idx = 0;
  525. size_t i;
  526. int ints[4];
  527. DEB(printk("NCR53c406a: Setup called\n");
  528. );
  529. if (setup_idx >= PORT_COUNT - 1) {
  530. printk("NCR53c406a: Setup called too many times. Bad LILO params?\n");
  531. return 0;
  532. }
  533. get_options(str, 4, ints);
  534. if (ints[0] < 1 || ints[0] > 3) {
  535. printk("NCR53c406a: Malformed command line\n");
  536. printk("NCR53c406a: Usage: ncr53c406a=<PORTBASE>[,<IRQ>[,<FASTPIO>]]\n");
  537. return 0;
  538. }
  539. for (i = 0; i < PORT_COUNT && !port_base; i++)
  540. if (ports[i] == ints[1]) {
  541. port_base = ints[1];
  542. DEB(printk("NCR53c406a: Specified port_base 0x%x\n", port_base);
  543. )
  544. }
  545. if (!port_base) {
  546. printk("NCR53c406a: Invalid PORTBASE 0x%x specified\n", ints[1]);
  547. return 0;
  548. }
  549. if (ints[0] > 1) {
  550. if (ints[2] == 0) {
  551. irq_level = 0;
  552. DEB(printk("NCR53c406a: Specified irq %d\n", irq_level);
  553. )
  554. } else
  555. for (i = 0; i < INTR_COUNT && irq_level < 0; i++)
  556. if (intrs[i] == ints[2]) {
  557. irq_level = ints[2];
  558. DEB(printk("NCR53c406a: Specified irq %d\n", port_base);
  559. )
  560. }
  561. if (irq_level < 0)
  562. printk("NCR53c406a: Invalid IRQ %d specified\n", ints[2]);
  563. }
  564. if (ints[0] > 2)
  565. fast_pio = ints[3];
  566. DEB(printk("NCR53c406a: port_base=0x%x, irq=%d, fast_pio=%d\n", port_base, irq_level, fast_pio);)
  567. return 1;
  568. }
  569. __setup("ncr53c406a=", NCR53c406a_setup);
  570. static const char *NCR53c406a_info(struct Scsi_Host *SChost)
  571. {
  572. DEB(printk("NCR53c406a_info called\n"));
  573. return (info_msg);
  574. }
  575. #if 0
  576. static void wait_intr(void)
  577. {
  578. unsigned long i = jiffies + WATCHDOG;
  579. while (time_after(i, jiffies) && !(inb(STAT_REG) & 0xe0)) { /* wait for a pseudo-interrupt */
  580. cpu_relax();
  581. barrier();
  582. }
  583. if (time_before_eq(i, jiffies)) { /* Timed out */
  584. rtrc(0);
  585. current_SC->result = DID_TIME_OUT << 16;
  586. current_SC->SCp.phase = idle;
  587. current_SC->scsi_done(current_SC);
  588. return;
  589. }
  590. NCR53c406a_intr(0, NULL, NULL);
  591. }
  592. #endif
  593. static int NCR53c406a_queue(Scsi_Cmnd * SCpnt, void (*done) (Scsi_Cmnd *))
  594. {
  595. int i;
  596. VDEB(printk("NCR53c406a_queue called\n"));
  597. 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));
  598. #if 0
  599. VDEB(for (i = 0; i < SCpnt->cmd_len; i++)
  600. printk("cmd[%d]=%02x ", i, SCpnt->cmnd[i]));
  601. VDEB(printk("\n"));
  602. #endif
  603. current_SC = SCpnt;
  604. current_SC->scsi_done = done;
  605. current_SC->SCp.phase = command_ph;
  606. current_SC->SCp.Status = 0;
  607. current_SC->SCp.Message = 0;
  608. /* We are locked here already by the mid layer */
  609. REG0;
  610. outb(SCpnt->device->id, DEST_ID); /* set destination */
  611. outb(FLUSH_FIFO, CMD_REG); /* reset the fifos */
  612. for (i = 0; i < SCpnt->cmd_len; i++) {
  613. outb(SCpnt->cmnd[i], SCSI_FIFO);
  614. }
  615. outb(SELECT_NO_ATN, CMD_REG);
  616. rtrc(1);
  617. return 0;
  618. }
  619. static int NCR53c406a_abort(Scsi_Cmnd * SCpnt)
  620. {
  621. DEB(printk("NCR53c406a_abort called\n"));
  622. return FAILED; /* Don't know how to abort */
  623. }
  624. static int NCR53c406a_host_reset(Scsi_Cmnd * SCpnt)
  625. {
  626. DEB(printk("NCR53c406a_reset called\n"));
  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. return SUCCESS;
  634. }
  635. static int NCR53c406a_device_reset(Scsi_Cmnd * SCpnt)
  636. {
  637. return FAILED;
  638. }
  639. static int NCR53c406a_bus_reset(Scsi_Cmnd * SCpnt)
  640. {
  641. return FAILED;
  642. }
  643. static int NCR53c406a_biosparm(struct scsi_device *disk,
  644. struct block_device *dev,
  645. sector_t capacity, int *info_array)
  646. {
  647. int size;
  648. DEB(printk("NCR53c406a_biosparm called\n"));
  649. size = capacity;
  650. info_array[0] = 64; /* heads */
  651. info_array[1] = 32; /* sectors */
  652. info_array[2] = size >> 11; /* cylinders */
  653. if (info_array[2] > 1024) { /* big disk */
  654. info_array[0] = 255;
  655. info_array[1] = 63;
  656. info_array[2] = size / (255 * 63);
  657. }
  658. return 0;
  659. }
  660. static irqreturn_t do_NCR53c406a_intr(int unused, void *dev_id,
  661. struct pt_regs *regs)
  662. {
  663. unsigned long flags;
  664. struct Scsi_Host *dev = dev_id;
  665. spin_lock_irqsave(dev->host_lock, flags);
  666. NCR53c406a_intr(0, dev_id, regs);
  667. spin_unlock_irqrestore(dev->host_lock, flags);
  668. return IRQ_HANDLED;
  669. }
  670. static void NCR53c406a_intr(int unused, void *dev_id, struct pt_regs *regs)
  671. {
  672. DEB(unsigned char fifo_size;
  673. )
  674. DEB(unsigned char seq_reg;
  675. )
  676. unsigned char status, int_reg;
  677. #if USE_PIO
  678. unsigned char pio_status;
  679. struct scatterlist *sglist;
  680. unsigned int sgcount;
  681. #endif
  682. VDEB(printk("NCR53c406a_intr called\n"));
  683. #if USE_PIO
  684. REG1;
  685. pio_status = inb(PIO_STATUS);
  686. #endif
  687. REG0;
  688. status = inb(STAT_REG);
  689. DEB(seq_reg = inb(SEQ_REG));
  690. int_reg = inb(INT_REG);
  691. DEB(fifo_size = inb(FIFO_FLAGS) & 0x1f);
  692. #if NCR53C406A_DEBUG
  693. printk("status=%02x, seq_reg=%02x, int_reg=%02x, fifo_size=%02x", status, seq_reg, int_reg, fifo_size);
  694. #if (USE_DMA)
  695. printk("\n");
  696. #else
  697. printk(", pio=%02x\n", pio_status);
  698. #endif /* USE_DMA */
  699. #endif /* NCR53C406A_DEBUG */
  700. if (int_reg & 0x80) { /* SCSI reset intr */
  701. rtrc(3);
  702. DEB(printk("NCR53c406a: reset intr received\n"));
  703. current_SC->SCp.phase = idle;
  704. current_SC->result = DID_RESET << 16;
  705. current_SC->scsi_done(current_SC);
  706. return;
  707. }
  708. #if USE_PIO
  709. if (pio_status & 0x80) {
  710. printk("NCR53C406A: Warning: PIO error!\n");
  711. current_SC->SCp.phase = idle;
  712. current_SC->result = DID_ERROR << 16;
  713. current_SC->scsi_done(current_SC);
  714. return;
  715. }
  716. #endif /* USE_PIO */
  717. if (status & 0x20) { /* Parity error */
  718. printk("NCR53c406a: Warning: parity error!\n");
  719. current_SC->SCp.phase = idle;
  720. current_SC->result = DID_PARITY << 16;
  721. current_SC->scsi_done(current_SC);
  722. return;
  723. }
  724. if (status & 0x40) { /* Gross error */
  725. printk("NCR53c406a: Warning: gross error!\n");
  726. current_SC->SCp.phase = idle;
  727. current_SC->result = DID_ERROR << 16;
  728. current_SC->scsi_done(current_SC);
  729. return;
  730. }
  731. if (int_reg & 0x20) { /* Disconnect */
  732. DEB(printk("NCR53c406a: disconnect intr received\n"));
  733. if (current_SC->SCp.phase != message_in) { /* Unexpected disconnect */
  734. current_SC->result = DID_NO_CONNECT << 16;
  735. } else { /* Command complete, return status and message */
  736. current_SC->result = (current_SC->SCp.Status & 0xff)
  737. | ((current_SC->SCp.Message & 0xff) << 8) | (DID_OK << 16);
  738. }
  739. rtrc(0);
  740. current_SC->SCp.phase = idle;
  741. current_SC->scsi_done(current_SC);
  742. return;
  743. }
  744. switch (status & 0x07) { /* scsi phase */
  745. case 0x00: /* DATA-OUT */
  746. if (int_reg & 0x10) { /* Target requesting info transfer */
  747. rtrc(5);
  748. current_SC->SCp.phase = data_out;
  749. VDEB(printk("NCR53c406a: Data-Out phase\n"));
  750. outb(FLUSH_FIFO, CMD_REG);
  751. LOAD_DMA_COUNT(current_SC->request_bufflen); /* Max transfer size */
  752. #if USE_DMA /* No s/g support for DMA */
  753. NCR53c406a_dma_write(current_SC->request_buffer, current_SC->request_bufflen);
  754. #endif /* USE_DMA */
  755. outb(TRANSFER_INFO | DMA_OP, CMD_REG);
  756. #if USE_PIO
  757. if (!current_SC->use_sg) /* Don't use scatter-gather */
  758. NCR53c406a_pio_write(current_SC->request_buffer, current_SC->request_bufflen);
  759. else { /* use scatter-gather */
  760. sgcount = current_SC->use_sg;
  761. sglist = current_SC->request_buffer;
  762. while (sgcount--) {
  763. NCR53c406a_pio_write(page_address(sglist->page) + sglist->offset, sglist->length);
  764. sglist++;
  765. }
  766. }
  767. REG0;
  768. #endif /* USE_PIO */
  769. }
  770. break;
  771. case 0x01: /* DATA-IN */
  772. if (int_reg & 0x10) { /* Target requesting info transfer */
  773. rtrc(6);
  774. current_SC->SCp.phase = data_in;
  775. VDEB(printk("NCR53c406a: Data-In phase\n"));
  776. outb(FLUSH_FIFO, CMD_REG);
  777. LOAD_DMA_COUNT(current_SC->request_bufflen); /* Max transfer size */
  778. #if USE_DMA /* No s/g support for DMA */
  779. NCR53c406a_dma_read(current_SC->request_buffer, current_SC->request_bufflen);
  780. #endif /* USE_DMA */
  781. outb(TRANSFER_INFO | DMA_OP, CMD_REG);
  782. #if USE_PIO
  783. if (!current_SC->use_sg) /* Don't use scatter-gather */
  784. NCR53c406a_pio_read(current_SC->request_buffer, current_SC->request_bufflen);
  785. else { /* Use scatter-gather */
  786. sgcount = current_SC->use_sg;
  787. sglist = current_SC->request_buffer;
  788. while (sgcount--) {
  789. NCR53c406a_pio_read(page_address(sglist->page) + sglist->offset, sglist->length);
  790. sglist++;
  791. }
  792. }
  793. REG0;
  794. #endif /* USE_PIO */
  795. }
  796. break;
  797. case 0x02: /* COMMAND */
  798. current_SC->SCp.phase = command_ph;
  799. printk("NCR53c406a: Warning: Unknown interrupt occurred in command phase!\n");
  800. break;
  801. case 0x03: /* STATUS */
  802. rtrc(7);
  803. current_SC->SCp.phase = status_ph;
  804. VDEB(printk("NCR53c406a: Status phase\n"));
  805. outb(FLUSH_FIFO, CMD_REG);
  806. outb(INIT_CMD_COMPLETE, CMD_REG);
  807. break;
  808. case 0x04: /* Reserved */
  809. case 0x05: /* Reserved */
  810. printk("NCR53c406a: WARNING: Reserved phase!!!\n");
  811. break;
  812. case 0x06: /* MESSAGE-OUT */
  813. DEB(printk("NCR53c406a: Message-Out phase\n"));
  814. current_SC->SCp.phase = message_out;
  815. outb(SET_ATN, CMD_REG); /* Reject the message */
  816. outb(MSG_ACCEPT, CMD_REG);
  817. break;
  818. case 0x07: /* MESSAGE-IN */
  819. rtrc(4);
  820. VDEB(printk("NCR53c406a: Message-In phase\n"));
  821. current_SC->SCp.phase = message_in;
  822. current_SC->SCp.Status = inb(SCSI_FIFO);
  823. current_SC->SCp.Message = inb(SCSI_FIFO);
  824. VDEB(printk("SCSI FIFO size=%d\n", inb(FIFO_FLAGS) & 0x1f));
  825. DEB(printk("Status = %02x Message = %02x\n", current_SC->SCp.Status, current_SC->SCp.Message));
  826. if (current_SC->SCp.Message == SAVE_POINTERS || current_SC->SCp.Message == DISCONNECT) {
  827. outb(SET_ATN, CMD_REG); /* Reject message */
  828. DEB(printk("Discarding SAVE_POINTERS message\n"));
  829. }
  830. outb(MSG_ACCEPT, CMD_REG);
  831. break;
  832. }
  833. }
  834. #ifndef IRQ_LEV
  835. static int irq_probe(void)
  836. {
  837. int irqs, irq;
  838. unsigned long i;
  839. inb(INT_REG); /* clear the interrupt register */
  840. irqs = probe_irq_on();
  841. /* Invalid command will cause an interrupt */
  842. REG0;
  843. outb(0xff, CMD_REG);
  844. /* Wait for the interrupt to occur */
  845. i = jiffies + WATCHDOG;
  846. while (time_after(i, jiffies) && !(inb(STAT_REG) & 0x80))
  847. barrier();
  848. if (time_before_eq(i, jiffies)) { /* Timed out, must be hardware trouble */
  849. probe_irq_off(irqs);
  850. return -1;
  851. }
  852. irq = probe_irq_off(irqs);
  853. /* Kick the chip */
  854. outb(CHIP_RESET, CMD_REG);
  855. outb(SCSI_NOP, CMD_REG);
  856. chip_init();
  857. return irq;
  858. }
  859. #endif /* IRQ_LEV */
  860. static void chip_init(void)
  861. {
  862. REG1;
  863. #if USE_DMA
  864. outb(0x00, PIO_STATUS);
  865. #else /* USE_PIO */
  866. outb(0x01, PIO_STATUS);
  867. #endif
  868. outb(0x00, PIO_FLAG);
  869. outb(C4_IMG, CONFIG4); /* REG0; */
  870. outb(C3_IMG, CONFIG3);
  871. outb(C2_IMG, CONFIG2);
  872. outb(C1_IMG, CONFIG1);
  873. outb(0x05, CLKCONV); /* clock conversion factor */
  874. outb(0x9C, SRTIMOUT); /* Selection timeout */
  875. outb(0x05, SYNCPRD); /* Synchronous transfer period */
  876. outb(SYNC_MODE, SYNCOFF); /* synchronous mode */
  877. }
  878. static void __init calc_port_addr(void)
  879. {
  880. /* Control Register Set 0 */
  881. TC_LSB = (port_base + 0x00);
  882. TC_MSB = (port_base + 0x01);
  883. SCSI_FIFO = (port_base + 0x02);
  884. CMD_REG = (port_base + 0x03);
  885. STAT_REG = (port_base + 0x04);
  886. DEST_ID = (port_base + 0x04);
  887. INT_REG = (port_base + 0x05);
  888. SRTIMOUT = (port_base + 0x05);
  889. SEQ_REG = (port_base + 0x06);
  890. SYNCPRD = (port_base + 0x06);
  891. FIFO_FLAGS = (port_base + 0x07);
  892. SYNCOFF = (port_base + 0x07);
  893. CONFIG1 = (port_base + 0x08);
  894. CLKCONV = (port_base + 0x09);
  895. /* TESTREG = (port_base+0x0A); */
  896. CONFIG2 = (port_base + 0x0B);
  897. CONFIG3 = (port_base + 0x0C);
  898. CONFIG4 = (port_base + 0x0D);
  899. TC_HIGH = (port_base + 0x0E);
  900. /* FIFO_BOTTOM = (port_base+0x0F); */
  901. /* Control Register Set 1 */
  902. /* JUMPER_SENSE = (port_base+0x00); */
  903. /* SRAM_PTR = (port_base+0x01); */
  904. /* SRAM_DATA = (port_base+0x02); */
  905. PIO_FIFO = (port_base + 0x04);
  906. /* PIO_FIFO1 = (port_base+0x05); */
  907. /* PIO_FIFO2 = (port_base+0x06); */
  908. /* PIO_FIFO3 = (port_base+0x07); */
  909. PIO_STATUS = (port_base + 0x08);
  910. /* ATA_CMD = (port_base+0x09); */
  911. /* ATA_ERR = (port_base+0x0A); */
  912. PIO_FLAG = (port_base + 0x0B);
  913. CONFIG5 = (port_base + 0x0D);
  914. /* SIGNATURE = (port_base+0x0E); */
  915. /* CONFIG6 = (port_base+0x0F); */
  916. }
  917. MODULE_LICENSE("GPL");
  918. /* NOTE: scatter-gather support only works in PIO mode.
  919. * Use SG_NONE if DMA mode is enabled!
  920. */
  921. static Scsi_Host_Template driver_template =
  922. {
  923. .proc_name = "NCR53c406a" /* proc_name */,
  924. .name = "NCR53c406a" /* name */,
  925. .detect = NCR53c406a_detect /* detect */,
  926. .release = NCR53c406a_release,
  927. .info = NCR53c406a_info /* info */,
  928. .queuecommand = NCR53c406a_queue /* queuecommand */,
  929. .eh_abort_handler = NCR53c406a_abort /* abort */,
  930. .eh_bus_reset_handler = NCR53c406a_bus_reset /* reset */,
  931. .eh_device_reset_handler = NCR53c406a_device_reset /* reset */,
  932. .eh_host_reset_handler = NCR53c406a_host_reset /* reset */,
  933. .bios_param = NCR53c406a_biosparm /* biosparm */,
  934. .can_queue = 1 /* can_queue */,
  935. .this_id = 7 /* SCSI ID of the chip */,
  936. .sg_tablesize = 32 /*SG_ALL*/ /*SG_NONE*/,
  937. .cmd_per_lun = 1 /* commands per lun */,
  938. .unchecked_isa_dma = 1 /* unchecked_isa_dma */,
  939. .use_clustering = ENABLE_CLUSTERING
  940. };
  941. #include "scsi_module.c"
  942. /*
  943. * Overrides for Emacs so that we get a uniform tabbing style.
  944. * Emacs will notice this stuff at the end of the file and automatically
  945. * adjust the settings for this buffer only. This must remain at the end
  946. * of the file.
  947. * ---------------------------------------------------------------------------
  948. * Local variables:
  949. * c-indent-level: 4
  950. * c-brace-imaginary-offset: 0
  951. * c-brace-offset: -4
  952. * c-argdecl-indent: 4
  953. * c-label-offset: -4
  954. * c-continued-statement-offset: 4
  955. * c-continued-brace-offset: 0
  956. * indent-tabs-mode: nil
  957. * tab-width: 8
  958. * End:
  959. */