NCR53c406a.c 28 KB

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