NCR53c406a.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097
  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(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. #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 (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(struct 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(scmd_id(SCpnt), 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_host_reset(Scsi_Cmnd * SCpnt)
  620. {
  621. DEB(printk("NCR53c406a_reset called\n"));
  622. spin_lock_irq(SCpnt->device->host->host_lock);
  623. outb(C4_IMG, CONFIG4); /* Select reg set 0 */
  624. outb(CHIP_RESET, CMD_REG);
  625. outb(SCSI_NOP, CMD_REG); /* required after reset */
  626. outb(SCSI_RESET, CMD_REG);
  627. chip_init();
  628. rtrc(2);
  629. spin_unlock_irq(SCpnt->device->host->host_lock);
  630. return SUCCESS;
  631. }
  632. static int NCR53c406a_biosparm(struct scsi_device *disk,
  633. struct block_device *dev,
  634. sector_t capacity, int *info_array)
  635. {
  636. int size;
  637. DEB(printk("NCR53c406a_biosparm called\n"));
  638. size = capacity;
  639. info_array[0] = 64; /* heads */
  640. info_array[1] = 32; /* sectors */
  641. info_array[2] = size >> 11; /* cylinders */
  642. if (info_array[2] > 1024) { /* big disk */
  643. info_array[0] = 255;
  644. info_array[1] = 63;
  645. info_array[2] = size / (255 * 63);
  646. }
  647. return 0;
  648. }
  649. static irqreturn_t do_NCR53c406a_intr(int unused, void *dev_id,
  650. struct pt_regs *regs)
  651. {
  652. unsigned long flags;
  653. struct Scsi_Host *dev = dev_id;
  654. spin_lock_irqsave(dev->host_lock, flags);
  655. NCR53c406a_intr(0, dev_id, regs);
  656. spin_unlock_irqrestore(dev->host_lock, flags);
  657. return IRQ_HANDLED;
  658. }
  659. static void NCR53c406a_intr(int unused, void *dev_id, struct pt_regs *regs)
  660. {
  661. DEB(unsigned char fifo_size;
  662. )
  663. DEB(unsigned char seq_reg;
  664. )
  665. unsigned char status, int_reg;
  666. #if USE_PIO
  667. unsigned char pio_status;
  668. struct scatterlist *sglist;
  669. unsigned int sgcount;
  670. #endif
  671. VDEB(printk("NCR53c406a_intr called\n"));
  672. #if USE_PIO
  673. REG1;
  674. pio_status = inb(PIO_STATUS);
  675. #endif
  676. REG0;
  677. status = inb(STAT_REG);
  678. DEB(seq_reg = inb(SEQ_REG));
  679. int_reg = inb(INT_REG);
  680. DEB(fifo_size = inb(FIFO_FLAGS) & 0x1f);
  681. #if NCR53C406A_DEBUG
  682. printk("status=%02x, seq_reg=%02x, int_reg=%02x, fifo_size=%02x", status, seq_reg, int_reg, fifo_size);
  683. #if (USE_DMA)
  684. printk("\n");
  685. #else
  686. printk(", pio=%02x\n", pio_status);
  687. #endif /* USE_DMA */
  688. #endif /* NCR53C406A_DEBUG */
  689. if (int_reg & 0x80) { /* SCSI reset intr */
  690. rtrc(3);
  691. DEB(printk("NCR53c406a: reset intr received\n"));
  692. current_SC->SCp.phase = idle;
  693. current_SC->result = DID_RESET << 16;
  694. current_SC->scsi_done(current_SC);
  695. return;
  696. }
  697. #if USE_PIO
  698. if (pio_status & 0x80) {
  699. printk("NCR53C406A: Warning: PIO error!\n");
  700. current_SC->SCp.phase = idle;
  701. current_SC->result = DID_ERROR << 16;
  702. current_SC->scsi_done(current_SC);
  703. return;
  704. }
  705. #endif /* USE_PIO */
  706. if (status & 0x20) { /* Parity error */
  707. printk("NCR53c406a: Warning: parity error!\n");
  708. current_SC->SCp.phase = idle;
  709. current_SC->result = DID_PARITY << 16;
  710. current_SC->scsi_done(current_SC);
  711. return;
  712. }
  713. if (status & 0x40) { /* Gross error */
  714. printk("NCR53c406a: Warning: gross error!\n");
  715. current_SC->SCp.phase = idle;
  716. current_SC->result = DID_ERROR << 16;
  717. current_SC->scsi_done(current_SC);
  718. return;
  719. }
  720. if (int_reg & 0x20) { /* Disconnect */
  721. DEB(printk("NCR53c406a: disconnect intr received\n"));
  722. if (current_SC->SCp.phase != message_in) { /* Unexpected disconnect */
  723. current_SC->result = DID_NO_CONNECT << 16;
  724. } else { /* Command complete, return status and message */
  725. current_SC->result = (current_SC->SCp.Status & 0xff)
  726. | ((current_SC->SCp.Message & 0xff) << 8) | (DID_OK << 16);
  727. }
  728. rtrc(0);
  729. current_SC->SCp.phase = idle;
  730. current_SC->scsi_done(current_SC);
  731. return;
  732. }
  733. switch (status & 0x07) { /* scsi phase */
  734. case 0x00: /* DATA-OUT */
  735. if (int_reg & 0x10) { /* Target requesting info transfer */
  736. rtrc(5);
  737. current_SC->SCp.phase = data_out;
  738. VDEB(printk("NCR53c406a: Data-Out phase\n"));
  739. outb(FLUSH_FIFO, CMD_REG);
  740. LOAD_DMA_COUNT(current_SC->request_bufflen); /* Max transfer size */
  741. #if USE_DMA /* No s/g support for DMA */
  742. NCR53c406a_dma_write(current_SC->request_buffer, current_SC->request_bufflen);
  743. #endif /* USE_DMA */
  744. outb(TRANSFER_INFO | DMA_OP, CMD_REG);
  745. #if USE_PIO
  746. if (!current_SC->use_sg) /* Don't use scatter-gather */
  747. NCR53c406a_pio_write(current_SC->request_buffer, current_SC->request_bufflen);
  748. else { /* use scatter-gather */
  749. sgcount = current_SC->use_sg;
  750. sglist = current_SC->request_buffer;
  751. while (sgcount--) {
  752. NCR53c406a_pio_write(page_address(sglist->page) + sglist->offset, sglist->length);
  753. sglist++;
  754. }
  755. }
  756. REG0;
  757. #endif /* USE_PIO */
  758. }
  759. break;
  760. case 0x01: /* DATA-IN */
  761. if (int_reg & 0x10) { /* Target requesting info transfer */
  762. rtrc(6);
  763. current_SC->SCp.phase = data_in;
  764. VDEB(printk("NCR53c406a: Data-In phase\n"));
  765. outb(FLUSH_FIFO, CMD_REG);
  766. LOAD_DMA_COUNT(current_SC->request_bufflen); /* Max transfer size */
  767. #if USE_DMA /* No s/g support for DMA */
  768. NCR53c406a_dma_read(current_SC->request_buffer, current_SC->request_bufflen);
  769. #endif /* USE_DMA */
  770. outb(TRANSFER_INFO | DMA_OP, CMD_REG);
  771. #if USE_PIO
  772. if (!current_SC->use_sg) /* Don't use scatter-gather */
  773. NCR53c406a_pio_read(current_SC->request_buffer, current_SC->request_bufflen);
  774. else { /* Use scatter-gather */
  775. sgcount = current_SC->use_sg;
  776. sglist = current_SC->request_buffer;
  777. while (sgcount--) {
  778. NCR53c406a_pio_read(page_address(sglist->page) + sglist->offset, sglist->length);
  779. sglist++;
  780. }
  781. }
  782. REG0;
  783. #endif /* USE_PIO */
  784. }
  785. break;
  786. case 0x02: /* COMMAND */
  787. current_SC->SCp.phase = command_ph;
  788. printk("NCR53c406a: Warning: Unknown interrupt occurred in command phase!\n");
  789. break;
  790. case 0x03: /* STATUS */
  791. rtrc(7);
  792. current_SC->SCp.phase = status_ph;
  793. VDEB(printk("NCR53c406a: Status phase\n"));
  794. outb(FLUSH_FIFO, CMD_REG);
  795. outb(INIT_CMD_COMPLETE, CMD_REG);
  796. break;
  797. case 0x04: /* Reserved */
  798. case 0x05: /* Reserved */
  799. printk("NCR53c406a: WARNING: Reserved phase!!!\n");
  800. break;
  801. case 0x06: /* MESSAGE-OUT */
  802. DEB(printk("NCR53c406a: Message-Out phase\n"));
  803. current_SC->SCp.phase = message_out;
  804. outb(SET_ATN, CMD_REG); /* Reject the message */
  805. outb(MSG_ACCEPT, CMD_REG);
  806. break;
  807. case 0x07: /* MESSAGE-IN */
  808. rtrc(4);
  809. VDEB(printk("NCR53c406a: Message-In phase\n"));
  810. current_SC->SCp.phase = message_in;
  811. current_SC->SCp.Status = inb(SCSI_FIFO);
  812. current_SC->SCp.Message = inb(SCSI_FIFO);
  813. VDEB(printk("SCSI FIFO size=%d\n", inb(FIFO_FLAGS) & 0x1f));
  814. DEB(printk("Status = %02x Message = %02x\n", current_SC->SCp.Status, current_SC->SCp.Message));
  815. if (current_SC->SCp.Message == SAVE_POINTERS || current_SC->SCp.Message == DISCONNECT) {
  816. outb(SET_ATN, CMD_REG); /* Reject message */
  817. DEB(printk("Discarding SAVE_POINTERS message\n"));
  818. }
  819. outb(MSG_ACCEPT, CMD_REG);
  820. break;
  821. }
  822. }
  823. #ifndef IRQ_LEV
  824. static int irq_probe(void)
  825. {
  826. int irqs, irq;
  827. unsigned long i;
  828. inb(INT_REG); /* clear the interrupt register */
  829. irqs = probe_irq_on();
  830. /* Invalid command will cause an interrupt */
  831. REG0;
  832. outb(0xff, CMD_REG);
  833. /* Wait for the interrupt to occur */
  834. i = jiffies + WATCHDOG;
  835. while (time_after(i, jiffies) && !(inb(STAT_REG) & 0x80))
  836. barrier();
  837. if (time_before_eq(i, jiffies)) { /* Timed out, must be hardware trouble */
  838. probe_irq_off(irqs);
  839. return -1;
  840. }
  841. irq = probe_irq_off(irqs);
  842. /* Kick the chip */
  843. outb(CHIP_RESET, CMD_REG);
  844. outb(SCSI_NOP, CMD_REG);
  845. chip_init();
  846. return irq;
  847. }
  848. #endif /* IRQ_LEV */
  849. static void chip_init(void)
  850. {
  851. REG1;
  852. #if USE_DMA
  853. outb(0x00, PIO_STATUS);
  854. #else /* USE_PIO */
  855. outb(0x01, PIO_STATUS);
  856. #endif
  857. outb(0x00, PIO_FLAG);
  858. outb(C4_IMG, CONFIG4); /* REG0; */
  859. outb(C3_IMG, CONFIG3);
  860. outb(C2_IMG, CONFIG2);
  861. outb(C1_IMG, CONFIG1);
  862. outb(0x05, CLKCONV); /* clock conversion factor */
  863. outb(0x9C, SRTIMOUT); /* Selection timeout */
  864. outb(0x05, SYNCPRD); /* Synchronous transfer period */
  865. outb(SYNC_MODE, SYNCOFF); /* synchronous mode */
  866. }
  867. static void __init calc_port_addr(void)
  868. {
  869. /* Control Register Set 0 */
  870. TC_LSB = (port_base + 0x00);
  871. TC_MSB = (port_base + 0x01);
  872. SCSI_FIFO = (port_base + 0x02);
  873. CMD_REG = (port_base + 0x03);
  874. STAT_REG = (port_base + 0x04);
  875. DEST_ID = (port_base + 0x04);
  876. INT_REG = (port_base + 0x05);
  877. SRTIMOUT = (port_base + 0x05);
  878. SEQ_REG = (port_base + 0x06);
  879. SYNCPRD = (port_base + 0x06);
  880. FIFO_FLAGS = (port_base + 0x07);
  881. SYNCOFF = (port_base + 0x07);
  882. CONFIG1 = (port_base + 0x08);
  883. CLKCONV = (port_base + 0x09);
  884. /* TESTREG = (port_base+0x0A); */
  885. CONFIG2 = (port_base + 0x0B);
  886. CONFIG3 = (port_base + 0x0C);
  887. CONFIG4 = (port_base + 0x0D);
  888. TC_HIGH = (port_base + 0x0E);
  889. /* FIFO_BOTTOM = (port_base+0x0F); */
  890. /* Control Register Set 1 */
  891. /* JUMPER_SENSE = (port_base+0x00); */
  892. /* SRAM_PTR = (port_base+0x01); */
  893. /* SRAM_DATA = (port_base+0x02); */
  894. PIO_FIFO = (port_base + 0x04);
  895. /* PIO_FIFO1 = (port_base+0x05); */
  896. /* PIO_FIFO2 = (port_base+0x06); */
  897. /* PIO_FIFO3 = (port_base+0x07); */
  898. PIO_STATUS = (port_base + 0x08);
  899. /* ATA_CMD = (port_base+0x09); */
  900. /* ATA_ERR = (port_base+0x0A); */
  901. PIO_FLAG = (port_base + 0x0B);
  902. CONFIG5 = (port_base + 0x0D);
  903. /* SIGNATURE = (port_base+0x0E); */
  904. /* CONFIG6 = (port_base+0x0F); */
  905. }
  906. MODULE_LICENSE("GPL");
  907. /* NOTE: scatter-gather support only works in PIO mode.
  908. * Use SG_NONE if DMA mode is enabled!
  909. */
  910. static struct scsi_host_template driver_template =
  911. {
  912. .proc_name = "NCR53c406a" /* proc_name */,
  913. .name = "NCR53c406a" /* name */,
  914. .detect = NCR53c406a_detect /* detect */,
  915. .release = NCR53c406a_release,
  916. .info = NCR53c406a_info /* info */,
  917. .queuecommand = NCR53c406a_queue /* queuecommand */,
  918. .eh_host_reset_handler = NCR53c406a_host_reset /* reset */,
  919. .bios_param = NCR53c406a_biosparm /* biosparm */,
  920. .can_queue = 1 /* can_queue */,
  921. .this_id = 7 /* SCSI ID of the chip */,
  922. .sg_tablesize = 32 /*SG_ALL*/ /*SG_NONE*/,
  923. .cmd_per_lun = 1 /* commands per lun */,
  924. .unchecked_isa_dma = 1 /* unchecked_isa_dma */,
  925. .use_clustering = ENABLE_CLUSTERING
  926. };
  927. #include "scsi_module.c"
  928. /*
  929. * Overrides for Emacs so that we get a uniform tabbing style.
  930. * Emacs will notice this stuff at the end of the file and automatically
  931. * adjust the settings for this buffer only. This must remain at the end
  932. * of the file.
  933. * ---------------------------------------------------------------------------
  934. * Local variables:
  935. * c-indent-level: 4
  936. * c-brace-imaginary-offset: 0
  937. * c-brace-offset: -4
  938. * c-argdecl-indent: 4
  939. * c-label-offset: -4
  940. * c-continued-statement-offset: 4
  941. * c-continued-brace-offset: 0
  942. * indent-tabs-mode: nil
  943. * tab-width: 8
  944. * End:
  945. */