sun3_scsi.c
上传用户:jlfgdled
上传日期:2013-04-10
资源大小:33168k
文件大小:14k
源码类别:

Linux/Unix编程

开发平台:

Unix_Linux

  1. /*
  2.  * Sun3 SCSI stuff by Erik Verbruggen (erik@bigmama.xtdnet.nl)
  3.  *
  4.  * Sun3 DMA routines added by Sam Creasey (sammy@oh.verio.com)
  5.  *
  6.  * Adapted from mac_scsinew.c:
  7.  */
  8. /*
  9.  * Generic Macintosh NCR5380 driver
  10.  *
  11.  * Copyright 1998, Michael Schmitz <mschmitz@lbl.gov>
  12.  *
  13.  * derived in part from:
  14.  */
  15. /*
  16.  * Generic Generic NCR5380 driver
  17.  *
  18.  * Copyright 1995, Russell King
  19.  *
  20.  * ALPHA RELEASE 1.
  21.  *
  22.  * For more information, please consult
  23.  *
  24.  * NCR 5380 Family
  25.  * SCSI Protocol Controller
  26.  * Databook
  27.  *
  28.  * NCR Microelectronics
  29.  * 1635 Aeroplaza Drive
  30.  * Colorado Springs, CO 80916
  31.  * 1+ (719) 578-3400
  32.  * 1+ (800) 334-5454
  33.  */
  34. /*
  35.  * This is from mac_scsi.h, but hey, maybe this is useful for Sun3 too! :)
  36.  *
  37.  * Options :
  38.  *
  39.  * PARITY - enable parity checking.  Not supported.
  40.  *
  41.  * SCSI2 - enable support for SCSI-II tagged queueing.  Untested.
  42.  *
  43.  * USLEEP - enable support for devices that don't disconnect.  Untested.
  44.  */
  45. /*
  46.  * $Log: sun3_NCR5380.c,v $
  47.  */
  48. #define AUTOSENSE
  49. #include <linux/types.h>
  50. #include <linux/stddef.h>
  51. #include <linux/ctype.h>
  52. #include <linux/delay.h>
  53. #include <linux/module.h>
  54. #include <linux/signal.h>
  55. #include <linux/sched.h>
  56. #include <linux/ioport.h>
  57. #include <linux/init.h>
  58. #include <linux/blk.h>
  59. #include <asm/io.h>
  60. #include <asm/system.h>
  61. #include <asm/sun3ints.h>
  62. #include <asm/dvma.h>
  63. #include <asm/idprom.h>
  64. #include <asm/machines.h>
  65. /* dma on! */
  66. #define REAL_DMA
  67. #include "scsi.h"
  68. #include "hosts.h"
  69. #include "sun3_scsi.h"
  70. #include "NCR5380.h"
  71. #include "constants.h"
  72. /* #define OLDDMA */
  73. #define USE_WRAPPER
  74. /*#define RESET_BOOT */
  75. #define DRIVER_SETUP
  76. #define NDEBUG 0
  77. /*
  78.  * BUG can be used to trigger a strange code-size related hang on 2.1 kernels
  79.  */
  80. #ifdef BUG
  81. #undef RESET_BOOT
  82. #undef DRIVER_SETUP
  83. #endif
  84. /* #define SUPPORT_TAGS */
  85. #define ENABLE_IRQ() enable_irq( IRQ_SUN3_SCSI ); 
  86. static void scsi_sun3_intr(int irq, void *dummy, struct pt_regs *fp);
  87. static inline unsigned char sun3scsi_read(int reg);
  88. static inline void sun3scsi_write(int reg, int value);
  89. static int setup_can_queue = -1;
  90. static int setup_cmd_per_lun = -1;
  91. static int setup_sg_tablesize = -1;
  92. #ifdef SUPPORT_TAGS
  93. static int setup_use_tagged_queuing = -1;
  94. #endif
  95. static int setup_hostid = -1;
  96. static Scsi_Cmnd *sun3_dma_setup_done = NULL;
  97. #define AFTER_RESET_DELAY (HZ/2)
  98. /* ms to wait after hitting dma regs */
  99. #define SUN3_DMA_DELAY 10
  100. /* dvma buffer to allocate -- 32k should hopefully be more than sufficient */
  101. #define SUN3_DVMA_BUFSIZE 0xe000
  102. /* minimum number of bytes to do dma on */
  103. #define SUN3_DMA_MINSIZE 128
  104. static volatile unsigned char *sun3_scsi_regp;
  105. static volatile struct sun3_dma_regs *dregs;
  106. #ifdef OLDDMA
  107. static unsigned char *dmabuf = NULL; /* dma memory buffer */
  108. #endif
  109. static struct sun3_udc_regs *udc_regs = NULL;
  110. static unsigned char *sun3_dma_orig_addr = NULL;
  111. static unsigned long sun3_dma_orig_count = 0;
  112. static int sun3_dma_active = 0;
  113. static unsigned long last_residual = 0;
  114. /*
  115.  * NCR 5380 register access functions
  116.  */
  117. static inline unsigned char sun3scsi_read(int reg)
  118. {
  119. return( sun3_scsi_regp[reg] );
  120. }
  121. static inline void sun3scsi_write(int reg, int value)
  122. {
  123. sun3_scsi_regp[reg] = value;
  124. }
  125. /* dma controller register access functions */
  126. static inline unsigned short sun3_udc_read(unsigned char reg)
  127. {
  128. unsigned short ret;
  129. dregs->udc_addr = UDC_CSR;
  130. udelay(SUN3_DMA_DELAY);
  131. ret = dregs->udc_data;
  132. udelay(SUN3_DMA_DELAY);
  133. return ret;
  134. }
  135. static inline void sun3_udc_write(unsigned short val, unsigned char reg)
  136. {
  137. dregs->udc_addr = reg;
  138. udelay(SUN3_DMA_DELAY);
  139. dregs->udc_data = val;
  140. udelay(SUN3_DMA_DELAY);
  141. }
  142. /*
  143.  * XXX: status debug
  144.  */
  145. static struct Scsi_Host *default_instance;
  146. /*
  147.  * Function : int sun3scsi_detect(Scsi_Host_Template * tpnt)
  148.  *
  149.  * Purpose : initializes mac NCR5380 driver based on the
  150.  * command line / compile time port and irq definitions.
  151.  *
  152.  * Inputs : tpnt - template for this SCSI adapter.
  153.  *
  154.  * Returns : 1 if a host adapter was found, 0 if not.
  155.  *
  156.  */
  157.  
  158. int sun3scsi_detect(Scsi_Host_Template * tpnt)
  159. {
  160. unsigned long ioaddr, iopte;
  161. int count = 0;
  162. static int called = 0;
  163. struct Scsi_Host *instance;
  164. /* check that this machine has an onboard 5380 */
  165. switch(idprom->id_machtype) {
  166. case SM_SUN3|SM_3_50:
  167. case SM_SUN3|SM_3_60:
  168. break;
  169. default:
  170. return 0;
  171. }
  172. if(called)
  173. return 0;
  174. tpnt->proc_name = "Sun3 5380 SCSI";
  175. /* setup variables */
  176. tpnt->can_queue =
  177. (setup_can_queue > 0) ? setup_can_queue : CAN_QUEUE;
  178. tpnt->cmd_per_lun =
  179. (setup_cmd_per_lun > 0) ? setup_cmd_per_lun : CMD_PER_LUN;
  180. tpnt->sg_tablesize = 
  181. (setup_sg_tablesize >= 0) ? setup_sg_tablesize : SG_TABLESIZE;
  182. if (setup_hostid >= 0)
  183. tpnt->this_id = setup_hostid;
  184. else {
  185. /* use 7 as default */
  186. tpnt->this_id = 7;
  187. }
  188. /* Taken from Sammy's lance driver: */
  189.         /* IOBASE_SUN3_SCSI can be found within the IO pmeg with some effort */
  190.         for(ioaddr = 0xfe00000; ioaddr < (0xfe00000 + SUN3_PMEG_SIZE);
  191.             ioaddr += SUN3_PTE_SIZE) {
  192.                 iopte = sun3_get_pte(ioaddr);
  193.                 if(!(iopte & SUN3_PAGE_TYPE_IO)) /* this an io page? */
  194.                         continue;
  195.                 if(((iopte & SUN3_PAGE_PGNUM_MASK) << PAGE_SHIFT) ==
  196.                    IOBASE_SUN3_SCSI) {
  197.                         count = 1;
  198.                         break;
  199.                 }
  200.         }
  201. if(!count) {
  202. printk("No Sun3 NCR5380 found!n");
  203. return 0;
  204. }
  205. sun3_scsi_regp = (unsigned char *)ioaddr;
  206. dregs = (struct sun3_dma_regs *)(((unsigned char *)ioaddr) + 8);
  207. if((udc_regs = dvma_malloc(sizeof(struct sun3_udc_regs)))
  208.    == NULL) {
  209.      printk("SUN3 Scsi couldn't allocate DVMA memory!n");
  210.      return 0;
  211. }
  212. #ifdef OLDDMA
  213. if((dmabuf = dvma_malloc_align(SUN3_DVMA_BUFSIZE, 0x10000)) == NULL) {
  214.      printk("SUN3 Scsi couldn't allocate DVMA memory!n");
  215.      return 0;
  216. }
  217. #endif
  218. #ifdef SUPPORT_TAGS
  219. if (setup_use_tagged_queuing < 0)
  220. setup_use_tagged_queuing = USE_TAGGED_QUEUING;
  221. #endif
  222. instance = scsi_register (tpnt, sizeof(struct NCR5380_hostdata));
  223. if(instance == NULL)
  224. return 0;
  225. default_instance = instance;
  226.         instance->io_port = (unsigned long) ioaddr;
  227. instance->irq = IRQ_SUN3_SCSI;
  228. NCR5380_init(instance, 0);
  229. instance->n_io_port = 32;
  230.         ((struct NCR5380_hostdata *)instance->hostdata)->ctrl = 0;
  231. if (request_irq(instance->irq, scsi_sun3_intr,
  232.      0, "Sun3SCSI-5380", NULL)) {
  233. #ifndef REAL_DMA
  234. printk("scsi%d: IRQ%d not free, interrupts disabledn",
  235.        instance->host_no, instance->irq);
  236. instance->irq = IRQ_NONE;
  237. #else
  238. printk("scsi%d: IRQ%d not free, bailing outn",
  239.        instance->host_no, instance->irq);
  240. return 0;
  241. #endif
  242. }
  243. printk("scsi%d: Sun3 5380 at port %lX irq", instance->host_no, instance->io_port);
  244. if (instance->irq == IRQ_NONE)
  245. printk ("s disabled");
  246. else
  247. printk (" %d", instance->irq);
  248. printk(" options CAN_QUEUE=%d CMD_PER_LUN=%d release=%d",
  249.        instance->can_queue, instance->cmd_per_lun,
  250.        SUN3SCSI_PUBLIC_RELEASE);
  251. printk("nscsi%d:", instance->host_no);
  252. NCR5380_print_options(instance);
  253. printk("n");
  254. dregs->csr = 0;
  255. udelay(SUN3_DMA_DELAY);
  256. dregs->csr = CSR_SCSI | CSR_FIFO | CSR_INTR;
  257. udelay(SUN3_DMA_DELAY);
  258. dregs->fifo_count = 0;
  259. called = 1;
  260. #ifdef RESET_BOOT
  261. sun3_scsi_reset_boot(instance);
  262. #endif
  263. return 1;
  264. }
  265. int sun3scsi_release (struct Scsi_Host *shpnt)
  266. {
  267. if (shpnt->irq != IRQ_NONE)
  268. free_irq (shpnt->irq, NULL);
  269. return 0;
  270. }
  271. #ifdef RESET_BOOT
  272. /*
  273.  * Our 'bus reset on boot' function
  274.  */
  275. static void sun3_scsi_reset_boot(struct Scsi_Host *instance)
  276. {
  277. unsigned long end;
  278. NCR5380_local_declare();
  279. NCR5380_setup(instance);
  280. /*
  281.  * Do a SCSI reset to clean up the bus during initialization. No
  282.  * messing with the queues, interrupts, or locks necessary here.
  283.  */
  284. printk( "Sun3 SCSI: resetting the SCSI bus..." );
  285. /* switch off SCSI IRQ - catch an interrupt without IRQ bit set else */
  286. //        sun3_disable_irq( IRQ_SUN3_SCSI );
  287. /* get in phase */
  288. NCR5380_write( TARGET_COMMAND_REG,
  289.       PHASE_SR_TO_TCR( NCR5380_read(STATUS_REG) ));
  290. /* assert RST */
  291. NCR5380_write( INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_RST );
  292. /* The min. reset hold time is 25us, so 40us should be enough */
  293. udelay( 50 );
  294. /* reset RST and interrupt */
  295. NCR5380_write( INITIATOR_COMMAND_REG, ICR_BASE );
  296. NCR5380_read( RESET_PARITY_INTERRUPT_REG );
  297. for( end = jiffies + AFTER_RESET_DELAY; time_before(jiffies, end); )
  298. barrier();
  299. /* switch on SCSI IRQ again */
  300. //        sun3_enable_irq( IRQ_SUN3_SCSI );
  301. printk( " donen" );
  302. }
  303. #endif
  304. const char * sun3scsi_info (struct Scsi_Host *spnt) {
  305.     return "";
  306. }
  307. // safe bits for the CSR
  308. #define CSR_GOOD 0x060f
  309. static void scsi_sun3_intr(int irq, void *dummy, struct pt_regs *fp)
  310. {
  311. unsigned short csr = dregs->csr;
  312. if(csr & ~CSR_GOOD) {
  313. if(csr & CSR_DMA_BUSERR) {
  314. printk("scsi%d: bus error in dman", default_instance->host_no);
  315. }
  316. if(csr & CSR_DMA_CONFLICT) {
  317. printk("scsi%d: dma conflictn", default_instance->host_no);
  318. }
  319. }
  320. if(csr & (CSR_SDB_INT | CSR_DMA_INT))
  321. NCR5380_intr(irq, dummy, fp);
  322. }
  323. /*
  324.  * Debug stuff - to be called on NMI, or sysrq key. Use at your own risk; 
  325.  * reentering NCR5380_print_status seems to have ugly side effects
  326.  */
  327. /* this doesn't seem to get used at all -- sam */
  328. #if 0
  329. void sun3_sun3_debug (void)
  330. {
  331. unsigned long flags;
  332. NCR5380_local_declare();
  333. if (default_instance) {
  334. save_flags(flags);
  335. cli();
  336. NCR5380_print_status(default_instance);
  337. restore_flags(flags);
  338. }
  339. }
  340. #endif
  341. /* sun3scsi_dma_setup() -- initialize the dma controller for a read/write */
  342. static unsigned long sun3scsi_dma_setup(void *data, unsigned long count, int write_flag)
  343. {
  344. #ifdef OLDDMA
  345. if(write_flag) 
  346. memcpy(dmabuf, data, count);
  347. else {
  348. sun3_dma_orig_addr = data;
  349. sun3_dma_orig_count = count;
  350. }
  351. #else
  352. void *addr;
  353. if(sun3_dma_orig_addr != NULL)
  354. dvma_unmap(sun3_dma_orig_addr);
  355. // addr = sun3_dvma_page((unsigned long)data, (unsigned long)dmabuf);
  356. addr = (void *)dvma_map((unsigned long) data, count);
  357. sun3_dma_orig_addr = addr;
  358. sun3_dma_orig_count = count;
  359. #endif
  360. dregs->fifo_count = 0;
  361. sun3_udc_write(UDC_RESET, UDC_CSR);
  362. /* reset fifo */
  363. dregs->csr &= ~CSR_FIFO;
  364. dregs->csr |= CSR_FIFO;
  365. /* set direction */
  366. if(write_flag)
  367. dregs->csr |= CSR_SEND;
  368. else
  369. dregs->csr &= ~CSR_SEND;
  370. /* byte count for fifo */
  371. dregs->fifo_count = count;
  372. sun3_udc_write(UDC_RESET, UDC_CSR);
  373. /* reset fifo */
  374. dregs->csr &= ~CSR_FIFO;
  375. dregs->csr |= CSR_FIFO;
  376. if(dregs->fifo_count != count) { 
  377. printk("scsi%d: fifo_mismatch %04x not %04xn",
  378.        default_instance->host_no, dregs->fifo_count,
  379.        (unsigned int) count);
  380. NCR5380_print(default_instance);
  381. }
  382. /* setup udc */
  383. #ifdef OLDDMA
  384. udc_regs->addr_hi = ((dvma_vtob(dmabuf) & 0xff0000) >> 8);
  385. udc_regs->addr_lo = (dvma_vtob(dmabuf) & 0xffff);
  386. #else
  387. udc_regs->addr_hi = (((unsigned long)(addr) & 0xff0000) >> 8);
  388. udc_regs->addr_lo = ((unsigned long)(addr) & 0xffff);
  389. #endif
  390. udc_regs->count = count/2; /* count in words */
  391. udc_regs->mode_hi = UDC_MODE_HIWORD;
  392. if(write_flag) {
  393. if(count & 1)
  394. udc_regs->count++;
  395. udc_regs->mode_lo = UDC_MODE_LSEND;
  396. udc_regs->rsel = UDC_RSEL_SEND;
  397. } else {
  398. udc_regs->mode_lo = UDC_MODE_LRECV;
  399. udc_regs->rsel = UDC_RSEL_RECV;
  400. }
  401. /* announce location of regs block */
  402. sun3_udc_write(((dvma_vtob(udc_regs) & 0xff0000) >> 8),
  403.        UDC_CHN_HI); 
  404. sun3_udc_write((dvma_vtob(udc_regs) & 0xffff), UDC_CHN_LO);
  405. /* set dma master on */
  406. sun3_udc_write(0xd, UDC_MODE);
  407. /* interrupt enable */
  408. sun3_udc_write(UDC_INT_ENABLE, UDC_CSR);
  409.         return count;
  410. }
  411. static inline unsigned long sun3scsi_dma_count(struct Scsi_Host *instance)
  412. {
  413. unsigned short resid;
  414. dregs->udc_addr = 0x32; 
  415. udelay(SUN3_DMA_DELAY);
  416. resid = dregs->udc_data;
  417. udelay(SUN3_DMA_DELAY);
  418. resid *= 2;
  419. return (unsigned long) resid;
  420. }
  421. static inline unsigned long sun3scsi_dma_residual(struct Scsi_Host *instance)
  422. {
  423. return last_residual;
  424. }
  425. static inline unsigned long sun3scsi_dma_xfer_len(unsigned long wanted, Scsi_Cmnd *cmd,
  426.     int write_flag)
  427. {
  428. if((cmd->request.cmd == 0) || (cmd->request.cmd == 1))
  429.   return wanted;
  430. else
  431. return 0;
  432. }
  433. /* clean up after our dma is done */
  434. static int sun3scsi_dma_finish(int write_flag)
  435. {
  436. unsigned short count;
  437. unsigned short fifo;
  438. int ret = 0;
  439. sun3_dma_active = 0;
  440. #if 1
  441. // check to empty the fifo on a read
  442. if(!write_flag) {
  443. int tmo = 20000; /* .2 sec */
  444. while(1) {
  445. if(dregs->csr & CSR_FIFO_EMPTY)
  446. break;
  447. if(--tmo <= 0) 
  448. return 1;
  449. udelay(10);
  450. }
  451. }
  452. #endif
  453. count = sun3scsi_dma_count(default_instance);
  454. #ifdef OLDDMA
  455. /* if we've finished a read, copy out the data we read */
  456.   if(sun3_dma_orig_addr) {
  457. /* check for residual bytes after dma end */
  458. if(count && (NCR5380_read(BUS_AND_STATUS_REG) &
  459.      (BASR_PHASE_MATCH | BASR_ACK))) {
  460. printk("scsi%d: sun3_scsi_finish: read overrun baby... ", default_instance->host_no);
  461. printk("basr now %02xn", NCR5380_read(BUS_AND_STATUS_REG));
  462. ret = count;
  463. }
  464. /* copy in what we dma'd no matter what */
  465. memcpy(sun3_dma_orig_addr, dmabuf, sun3_dma_orig_count);
  466. sun3_dma_orig_addr = NULL;
  467. }
  468. #else
  469. fifo = dregs->fifo_count;
  470. last_residual = fifo;
  471. /* empty bytes from the fifo which didn't make it */
  472. if((!write_flag) && (count - fifo) == 2) {
  473. unsigned short data;
  474. unsigned char *vaddr;
  475. data = dregs->fifo_data;
  476. vaddr = (unsigned char *)dvma_btov(sun3_dma_orig_addr);
  477. vaddr += (sun3_dma_orig_count - fifo);
  478. vaddr[-2] = (data & 0xff00) >> 8;
  479. vaddr[-1] = (data & 0xff);
  480. }
  481. dvma_unmap(sun3_dma_orig_addr);
  482. sun3_dma_orig_addr = NULL;
  483. #endif
  484. sun3_udc_write(UDC_RESET, UDC_CSR);
  485. dregs->fifo_count = 0;
  486. dregs->csr &= ~CSR_SEND;
  487. /* reset fifo */
  488. dregs->csr &= ~CSR_FIFO;
  489. dregs->csr |= CSR_FIFO;
  490. sun3_dma_setup_done = NULL;
  491. return ret;
  492. }
  493. #include "sun3_NCR5380.c"
  494. static Scsi_Host_Template driver_template = SUN3_NCR5380;
  495. #include "scsi_module.c"
  496. MODULE_LICENSE("GPL");