sun3_scsi.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642
  1. /*
  2. * Sun3 SCSI stuff by Erik Verbruggen (erik@bigmama.xtdnet.nl)
  3. *
  4. * Sun3 DMA routines added by Sam Creasey (sammy@sammy.net)
  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/blkdev.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 <scsi/scsi_host.h>
  69. #include "sun3_scsi.h"
  70. #include "NCR5380.h"
  71. static void NCR5380_print(struct Scsi_Host *instance);
  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 irqreturn_t 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. module_param(setup_can_queue, int, 0);
  91. static int setup_cmd_per_lun = -1;
  92. module_param(setup_cmd_per_lun, int, 0);
  93. static int setup_sg_tablesize = -1;
  94. module_param(setup_sg_tablesize, int, 0);
  95. #ifdef SUPPORT_TAGS
  96. static int setup_use_tagged_queuing = -1;
  97. module_param(setup_use_tagged_queuing, int, 0);
  98. #endif
  99. static int setup_hostid = -1;
  100. module_param(setup_hostid, int, 0);
  101. static Scsi_Cmnd *sun3_dma_setup_done = NULL;
  102. #define AFTER_RESET_DELAY (HZ/2)
  103. /* ms to wait after hitting dma regs */
  104. #define SUN3_DMA_DELAY 10
  105. /* dvma buffer to allocate -- 32k should hopefully be more than sufficient */
  106. #define SUN3_DVMA_BUFSIZE 0xe000
  107. /* minimum number of bytes to do dma on */
  108. #define SUN3_DMA_MINSIZE 128
  109. static volatile unsigned char *sun3_scsi_regp;
  110. static volatile struct sun3_dma_regs *dregs;
  111. #ifdef OLDDMA
  112. static unsigned char *dmabuf = NULL; /* dma memory buffer */
  113. #endif
  114. static struct sun3_udc_regs *udc_regs = NULL;
  115. static unsigned char *sun3_dma_orig_addr = NULL;
  116. static unsigned long sun3_dma_orig_count = 0;
  117. static int sun3_dma_active = 0;
  118. static unsigned long last_residual = 0;
  119. /*
  120. * NCR 5380 register access functions
  121. */
  122. static inline unsigned char sun3scsi_read(int reg)
  123. {
  124. return( sun3_scsi_regp[reg] );
  125. }
  126. static inline void sun3scsi_write(int reg, int value)
  127. {
  128. sun3_scsi_regp[reg] = value;
  129. }
  130. /* dma controller register access functions */
  131. static inline unsigned short sun3_udc_read(unsigned char reg)
  132. {
  133. unsigned short ret;
  134. dregs->udc_addr = UDC_CSR;
  135. udelay(SUN3_DMA_DELAY);
  136. ret = dregs->udc_data;
  137. udelay(SUN3_DMA_DELAY);
  138. return ret;
  139. }
  140. static inline void sun3_udc_write(unsigned short val, unsigned char reg)
  141. {
  142. dregs->udc_addr = reg;
  143. udelay(SUN3_DMA_DELAY);
  144. dregs->udc_data = val;
  145. udelay(SUN3_DMA_DELAY);
  146. }
  147. /*
  148. * XXX: status debug
  149. */
  150. static struct Scsi_Host *default_instance;
  151. /*
  152. * Function : int sun3scsi_detect(Scsi_Host_Template * tpnt)
  153. *
  154. * Purpose : initializes mac NCR5380 driver based on the
  155. * command line / compile time port and irq definitions.
  156. *
  157. * Inputs : tpnt - template for this SCSI adapter.
  158. *
  159. * Returns : 1 if a host adapter was found, 0 if not.
  160. *
  161. */
  162. int sun3scsi_detect(Scsi_Host_Template * tpnt)
  163. {
  164. unsigned long ioaddr;
  165. static int called = 0;
  166. struct Scsi_Host *instance;
  167. /* check that this machine has an onboard 5380 */
  168. switch(idprom->id_machtype) {
  169. case SM_SUN3|SM_3_50:
  170. case SM_SUN3|SM_3_60:
  171. break;
  172. default:
  173. return 0;
  174. }
  175. if(called)
  176. return 0;
  177. tpnt->proc_name = "Sun3 5380 SCSI";
  178. /* setup variables */
  179. tpnt->can_queue =
  180. (setup_can_queue > 0) ? setup_can_queue : CAN_QUEUE;
  181. tpnt->cmd_per_lun =
  182. (setup_cmd_per_lun > 0) ? setup_cmd_per_lun : CMD_PER_LUN;
  183. tpnt->sg_tablesize =
  184. (setup_sg_tablesize >= 0) ? setup_sg_tablesize : SG_TABLESIZE;
  185. if (setup_hostid >= 0)
  186. tpnt->this_id = setup_hostid;
  187. else {
  188. /* use 7 as default */
  189. tpnt->this_id = 7;
  190. }
  191. ioaddr = (unsigned long)ioremap(IOBASE_SUN3_SCSI, PAGE_SIZE);
  192. sun3_scsi_regp = (unsigned char *)ioaddr;
  193. dregs = (struct sun3_dma_regs *)(((unsigned char *)ioaddr) + 8);
  194. if((udc_regs = dvma_malloc(sizeof(struct sun3_udc_regs)))
  195. == NULL) {
  196. printk("SUN3 Scsi couldn't allocate DVMA memory!\n");
  197. return 0;
  198. }
  199. #ifdef OLDDMA
  200. if((dmabuf = dvma_malloc_align(SUN3_DVMA_BUFSIZE, 0x10000)) == NULL) {
  201. printk("SUN3 Scsi couldn't allocate DVMA memory!\n");
  202. return 0;
  203. }
  204. #endif
  205. #ifdef SUPPORT_TAGS
  206. if (setup_use_tagged_queuing < 0)
  207. setup_use_tagged_queuing = USE_TAGGED_QUEUING;
  208. #endif
  209. instance = scsi_register (tpnt, sizeof(struct NCR5380_hostdata));
  210. if(instance == NULL)
  211. return 0;
  212. default_instance = instance;
  213. instance->io_port = (unsigned long) ioaddr;
  214. instance->irq = IRQ_SUN3_SCSI;
  215. NCR5380_init(instance, 0);
  216. instance->n_io_port = 32;
  217. ((struct NCR5380_hostdata *)instance->hostdata)->ctrl = 0;
  218. if (request_irq(instance->irq, scsi_sun3_intr,
  219. 0, "Sun3SCSI-5380", NULL)) {
  220. #ifndef REAL_DMA
  221. printk("scsi%d: IRQ%d not free, interrupts disabled\n",
  222. instance->host_no, instance->irq);
  223. instance->irq = SCSI_IRQ_NONE;
  224. #else
  225. printk("scsi%d: IRQ%d not free, bailing out\n",
  226. instance->host_no, instance->irq);
  227. return 0;
  228. #endif
  229. }
  230. printk("scsi%d: Sun3 5380 at port %lX irq", instance->host_no, instance->io_port);
  231. if (instance->irq == SCSI_IRQ_NONE)
  232. printk ("s disabled");
  233. else
  234. printk (" %d", instance->irq);
  235. printk(" options CAN_QUEUE=%d CMD_PER_LUN=%d release=%d",
  236. instance->can_queue, instance->cmd_per_lun,
  237. SUN3SCSI_PUBLIC_RELEASE);
  238. printk("\nscsi%d:", instance->host_no);
  239. NCR5380_print_options(instance);
  240. printk("\n");
  241. dregs->csr = 0;
  242. udelay(SUN3_DMA_DELAY);
  243. dregs->csr = CSR_SCSI | CSR_FIFO | CSR_INTR;
  244. udelay(SUN3_DMA_DELAY);
  245. dregs->fifo_count = 0;
  246. called = 1;
  247. #ifdef RESET_BOOT
  248. sun3_scsi_reset_boot(instance);
  249. #endif
  250. return 1;
  251. }
  252. int sun3scsi_release (struct Scsi_Host *shpnt)
  253. {
  254. if (shpnt->irq != SCSI_IRQ_NONE)
  255. free_irq (shpnt->irq, NULL);
  256. iounmap((void *)sun3_scsi_regp);
  257. return 0;
  258. }
  259. #ifdef RESET_BOOT
  260. /*
  261. * Our 'bus reset on boot' function
  262. */
  263. static void sun3_scsi_reset_boot(struct Scsi_Host *instance)
  264. {
  265. unsigned long end;
  266. NCR5380_local_declare();
  267. NCR5380_setup(instance);
  268. /*
  269. * Do a SCSI reset to clean up the bus during initialization. No
  270. * messing with the queues, interrupts, or locks necessary here.
  271. */
  272. printk( "Sun3 SCSI: resetting the SCSI bus..." );
  273. /* switch off SCSI IRQ - catch an interrupt without IRQ bit set else */
  274. // sun3_disable_irq( IRQ_SUN3_SCSI );
  275. /* get in phase */
  276. NCR5380_write( TARGET_COMMAND_REG,
  277. PHASE_SR_TO_TCR( NCR5380_read(STATUS_REG) ));
  278. /* assert RST */
  279. NCR5380_write( INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_RST );
  280. /* The min. reset hold time is 25us, so 40us should be enough */
  281. udelay( 50 );
  282. /* reset RST and interrupt */
  283. NCR5380_write( INITIATOR_COMMAND_REG, ICR_BASE );
  284. NCR5380_read( RESET_PARITY_INTERRUPT_REG );
  285. for( end = jiffies + AFTER_RESET_DELAY; time_before(jiffies, end); )
  286. barrier();
  287. /* switch on SCSI IRQ again */
  288. // sun3_enable_irq( IRQ_SUN3_SCSI );
  289. printk( " done\n" );
  290. }
  291. #endif
  292. const char * sun3scsi_info (struct Scsi_Host *spnt) {
  293. return "";
  294. }
  295. // safe bits for the CSR
  296. #define CSR_GOOD 0x060f
  297. static irqreturn_t scsi_sun3_intr(int irq, void *dummy, struct pt_regs *fp)
  298. {
  299. unsigned short csr = dregs->csr;
  300. int handled = 0;
  301. if(csr & ~CSR_GOOD) {
  302. if(csr & CSR_DMA_BUSERR) {
  303. printk("scsi%d: bus error in dma\n", default_instance->host_no);
  304. }
  305. if(csr & CSR_DMA_CONFLICT) {
  306. printk("scsi%d: dma conflict\n", default_instance->host_no);
  307. }
  308. handled = 1;
  309. }
  310. if(csr & (CSR_SDB_INT | CSR_DMA_INT)) {
  311. NCR5380_intr(irq, dummy, fp);
  312. handled = 1;
  313. }
  314. return IRQ_RETVAL(handled);
  315. }
  316. /*
  317. * Debug stuff - to be called on NMI, or sysrq key. Use at your own risk;
  318. * reentering NCR5380_print_status seems to have ugly side effects
  319. */
  320. /* this doesn't seem to get used at all -- sam */
  321. #if 0
  322. void sun3_sun3_debug (void)
  323. {
  324. unsigned long flags;
  325. NCR5380_local_declare();
  326. if (default_instance) {
  327. local_irq_save(flags);
  328. NCR5380_print_status(default_instance);
  329. local_irq_restore(flags);
  330. }
  331. }
  332. #endif
  333. /* sun3scsi_dma_setup() -- initialize the dma controller for a read/write */
  334. static unsigned long sun3scsi_dma_setup(void *data, unsigned long count, int write_flag)
  335. {
  336. #ifdef OLDDMA
  337. if(write_flag)
  338. memcpy(dmabuf, data, count);
  339. else {
  340. sun3_dma_orig_addr = data;
  341. sun3_dma_orig_count = count;
  342. }
  343. #else
  344. void *addr;
  345. if(sun3_dma_orig_addr != NULL)
  346. dvma_unmap(sun3_dma_orig_addr);
  347. // addr = sun3_dvma_page((unsigned long)data, (unsigned long)dmabuf);
  348. addr = (void *)dvma_map((unsigned long) data, count);
  349. sun3_dma_orig_addr = addr;
  350. sun3_dma_orig_count = count;
  351. #endif
  352. dregs->fifo_count = 0;
  353. sun3_udc_write(UDC_RESET, UDC_CSR);
  354. /* reset fifo */
  355. dregs->csr &= ~CSR_FIFO;
  356. dregs->csr |= CSR_FIFO;
  357. /* set direction */
  358. if(write_flag)
  359. dregs->csr |= CSR_SEND;
  360. else
  361. dregs->csr &= ~CSR_SEND;
  362. /* byte count for fifo */
  363. dregs->fifo_count = count;
  364. sun3_udc_write(UDC_RESET, UDC_CSR);
  365. /* reset fifo */
  366. dregs->csr &= ~CSR_FIFO;
  367. dregs->csr |= CSR_FIFO;
  368. if(dregs->fifo_count != count) {
  369. printk("scsi%d: fifo_mismatch %04x not %04x\n",
  370. default_instance->host_no, dregs->fifo_count,
  371. (unsigned int) count);
  372. NCR5380_print(default_instance);
  373. }
  374. /* setup udc */
  375. #ifdef OLDDMA
  376. udc_regs->addr_hi = ((dvma_vtob(dmabuf) & 0xff0000) >> 8);
  377. udc_regs->addr_lo = (dvma_vtob(dmabuf) & 0xffff);
  378. #else
  379. udc_regs->addr_hi = (((unsigned long)(addr) & 0xff0000) >> 8);
  380. udc_regs->addr_lo = ((unsigned long)(addr) & 0xffff);
  381. #endif
  382. udc_regs->count = count/2; /* count in words */
  383. udc_regs->mode_hi = UDC_MODE_HIWORD;
  384. if(write_flag) {
  385. if(count & 1)
  386. udc_regs->count++;
  387. udc_regs->mode_lo = UDC_MODE_LSEND;
  388. udc_regs->rsel = UDC_RSEL_SEND;
  389. } else {
  390. udc_regs->mode_lo = UDC_MODE_LRECV;
  391. udc_regs->rsel = UDC_RSEL_RECV;
  392. }
  393. /* announce location of regs block */
  394. sun3_udc_write(((dvma_vtob(udc_regs) & 0xff0000) >> 8),
  395. UDC_CHN_HI);
  396. sun3_udc_write((dvma_vtob(udc_regs) & 0xffff), UDC_CHN_LO);
  397. /* set dma master on */
  398. sun3_udc_write(0xd, UDC_MODE);
  399. /* interrupt enable */
  400. sun3_udc_write(UDC_INT_ENABLE, UDC_CSR);
  401. return count;
  402. }
  403. static inline unsigned long sun3scsi_dma_count(struct Scsi_Host *instance)
  404. {
  405. unsigned short resid;
  406. dregs->udc_addr = 0x32;
  407. udelay(SUN3_DMA_DELAY);
  408. resid = dregs->udc_data;
  409. udelay(SUN3_DMA_DELAY);
  410. resid *= 2;
  411. return (unsigned long) resid;
  412. }
  413. static inline unsigned long sun3scsi_dma_residual(struct Scsi_Host *instance)
  414. {
  415. return last_residual;
  416. }
  417. static inline unsigned long sun3scsi_dma_xfer_len(unsigned long wanted, Scsi_Cmnd *cmd,
  418. int write_flag)
  419. {
  420. if(cmd->request->flags & REQ_CMD)
  421. return wanted;
  422. else
  423. return 0;
  424. }
  425. static inline int sun3scsi_dma_start(unsigned long count, unsigned char *data)
  426. {
  427. sun3_udc_write(UDC_CHN_START, UDC_CSR);
  428. return 0;
  429. }
  430. /* clean up after our dma is done */
  431. static int sun3scsi_dma_finish(int write_flag)
  432. {
  433. unsigned short count;
  434. unsigned short fifo;
  435. int ret = 0;
  436. sun3_dma_active = 0;
  437. #if 1
  438. // check to empty the fifo on a read
  439. if(!write_flag) {
  440. int tmo = 20000; /* .2 sec */
  441. while(1) {
  442. if(dregs->csr & CSR_FIFO_EMPTY)
  443. break;
  444. if(--tmo <= 0) {
  445. printk("sun3scsi: fifo failed to empty!\n");
  446. return 1;
  447. }
  448. udelay(10);
  449. }
  450. }
  451. #endif
  452. count = sun3scsi_dma_count(default_instance);
  453. #ifdef OLDDMA
  454. /* if we've finished a read, copy out the data we read */
  455. if(sun3_dma_orig_addr) {
  456. /* check for residual bytes after dma end */
  457. if(count && (NCR5380_read(BUS_AND_STATUS_REG) &
  458. (BASR_PHASE_MATCH | BASR_ACK))) {
  459. printk("scsi%d: sun3_scsi_finish: read overrun baby... ", default_instance->host_no);
  460. printk("basr now %02x\n", NCR5380_read(BUS_AND_STATUS_REG));
  461. ret = count;
  462. }
  463. /* copy in what we dma'd no matter what */
  464. memcpy(sun3_dma_orig_addr, dmabuf, sun3_dma_orig_count);
  465. sun3_dma_orig_addr = NULL;
  466. }
  467. #else
  468. fifo = dregs->fifo_count;
  469. last_residual = fifo;
  470. /* empty bytes from the fifo which didn't make it */
  471. if((!write_flag) && (count - fifo) == 2) {
  472. unsigned short data;
  473. unsigned char *vaddr;
  474. data = dregs->fifo_data;
  475. vaddr = (unsigned char *)dvma_btov(sun3_dma_orig_addr);
  476. vaddr += (sun3_dma_orig_count - fifo);
  477. vaddr[-2] = (data & 0xff00) >> 8;
  478. vaddr[-1] = (data & 0xff);
  479. }
  480. dvma_unmap(sun3_dma_orig_addr);
  481. sun3_dma_orig_addr = NULL;
  482. #endif
  483. sun3_udc_write(UDC_RESET, UDC_CSR);
  484. dregs->fifo_count = 0;
  485. dregs->csr &= ~CSR_SEND;
  486. /* reset fifo */
  487. dregs->csr &= ~CSR_FIFO;
  488. dregs->csr |= CSR_FIFO;
  489. sun3_dma_setup_done = NULL;
  490. return ret;
  491. }
  492. #include "sun3_NCR5380.c"
  493. static Scsi_Host_Template driver_template = {
  494. .name = SUN3_SCSI_NAME,
  495. .detect = sun3scsi_detect,
  496. .release = sun3scsi_release,
  497. .info = sun3scsi_info,
  498. .queuecommand = sun3scsi_queue_command,
  499. .eh_abort_handler = sun3scsi_abort,
  500. .eh_bus_reset_handler = sun3scsi_bus_reset,
  501. .can_queue = CAN_QUEUE,
  502. .this_id = 7,
  503. .sg_tablesize = SG_TABLESIZE,
  504. .cmd_per_lun = CMD_PER_LUN,
  505. .use_clustering = DISABLE_CLUSTERING
  506. };
  507. #include "scsi_module.c"
  508. MODULE_LICENSE("GPL");