sun3_scsi.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643
  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/ioport.h>
  56. #include <linux/init.h>
  57. #include <linux/blkdev.h>
  58. #include <asm/io.h>
  59. #include <asm/system.h>
  60. #include <asm/sun3ints.h>
  61. #include <asm/dvma.h>
  62. #include <asm/idprom.h>
  63. #include <asm/machines.h>
  64. /* dma on! */
  65. #define REAL_DMA
  66. #include "scsi.h"
  67. #include "initio.h"
  68. #include <scsi/scsi_host.h>
  69. #include "sun3_scsi.h"
  70. static void NCR5380_print(struct Scsi_Host *instance);
  71. /* #define OLDDMA */
  72. #define USE_WRAPPER
  73. /*#define RESET_BOOT */
  74. #define DRIVER_SETUP
  75. #define NDEBUG 0
  76. /*
  77. * BUG can be used to trigger a strange code-size related hang on 2.1 kernels
  78. */
  79. #ifdef BUG
  80. #undef RESET_BOOT
  81. #undef DRIVER_SETUP
  82. #endif
  83. /* #define SUPPORT_TAGS */
  84. #define ENABLE_IRQ() enable_irq( IRQ_SUN3_SCSI );
  85. static irqreturn_t scsi_sun3_intr(int irq, void *dummy);
  86. static inline unsigned char sun3scsi_read(int reg);
  87. static inline void sun3scsi_write(int reg, int value);
  88. static int setup_can_queue = -1;
  89. module_param(setup_can_queue, int, 0);
  90. static int setup_cmd_per_lun = -1;
  91. module_param(setup_cmd_per_lun, int, 0);
  92. static int setup_sg_tablesize = -1;
  93. module_param(setup_sg_tablesize, int, 0);
  94. #ifdef SUPPORT_TAGS
  95. static int setup_use_tagged_queuing = -1;
  96. module_param(setup_use_tagged_queuing, int, 0);
  97. #endif
  98. static int setup_hostid = -1;
  99. module_param(setup_hostid, int, 0);
  100. static struct scsi_cmnd *sun3_dma_setup_done = NULL;
  101. #define AFTER_RESET_DELAY (HZ/2)
  102. /* ms to wait after hitting dma regs */
  103. #define SUN3_DMA_DELAY 10
  104. /* dvma buffer to allocate -- 32k should hopefully be more than sufficient */
  105. #define SUN3_DVMA_BUFSIZE 0xe000
  106. /* minimum number of bytes to do dma on */
  107. #define SUN3_DMA_MINSIZE 128
  108. static volatile unsigned char *sun3_scsi_regp;
  109. static volatile struct sun3_dma_regs *dregs;
  110. #ifdef OLDDMA
  111. static unsigned char *dmabuf = NULL; /* dma memory buffer */
  112. #endif
  113. static struct sun3_udc_regs *udc_regs = NULL;
  114. static unsigned char *sun3_dma_orig_addr = NULL;
  115. static unsigned long sun3_dma_orig_count = 0;
  116. static int sun3_dma_active = 0;
  117. static unsigned long last_residual = 0;
  118. /*
  119. * NCR 5380 register access functions
  120. */
  121. static inline unsigned char sun3scsi_read(int reg)
  122. {
  123. return( sun3_scsi_regp[reg] );
  124. }
  125. static inline void sun3scsi_write(int reg, int value)
  126. {
  127. sun3_scsi_regp[reg] = value;
  128. }
  129. /* dma controller register access functions */
  130. static inline unsigned short sun3_udc_read(unsigned char reg)
  131. {
  132. unsigned short ret;
  133. dregs->udc_addr = UDC_CSR;
  134. udelay(SUN3_DMA_DELAY);
  135. ret = dregs->udc_data;
  136. udelay(SUN3_DMA_DELAY);
  137. return ret;
  138. }
  139. static inline void sun3_udc_write(unsigned short val, unsigned char reg)
  140. {
  141. dregs->udc_addr = reg;
  142. udelay(SUN3_DMA_DELAY);
  143. dregs->udc_data = val;
  144. udelay(SUN3_DMA_DELAY);
  145. }
  146. /*
  147. * XXX: status debug
  148. */
  149. static struct Scsi_Host *default_instance;
  150. /*
  151. * Function : int sun3scsi_detect(struct scsi_host_template * tpnt)
  152. *
  153. * Purpose : initializes mac NCR5380 driver based on the
  154. * command line / compile time port and irq definitions.
  155. *
  156. * Inputs : tpnt - template for this SCSI adapter.
  157. *
  158. * Returns : 1 if a host adapter was found, 0 if not.
  159. *
  160. */
  161. int sun3scsi_detect(struct scsi_host_template * tpnt)
  162. {
  163. unsigned long ioaddr;
  164. static int called = 0;
  165. struct Scsi_Host *instance;
  166. /* check that this machine has an onboard 5380 */
  167. switch(idprom->id_machtype) {
  168. case SM_SUN3|SM_3_50:
  169. case SM_SUN3|SM_3_60:
  170. break;
  171. default:
  172. return 0;
  173. }
  174. if(called)
  175. return 0;
  176. tpnt->proc_name = "Sun3 5380 SCSI";
  177. /* setup variables */
  178. tpnt->can_queue =
  179. (setup_can_queue > 0) ? setup_can_queue : CAN_QUEUE;
  180. tpnt->cmd_per_lun =
  181. (setup_cmd_per_lun > 0) ? setup_cmd_per_lun : CMD_PER_LUN;
  182. tpnt->sg_tablesize =
  183. (setup_sg_tablesize >= 0) ? setup_sg_tablesize : SG_TABLESIZE;
  184. if (setup_hostid >= 0)
  185. tpnt->this_id = setup_hostid;
  186. else {
  187. /* use 7 as default */
  188. tpnt->this_id = 7;
  189. }
  190. ioaddr = (unsigned long)ioremap(IOBASE_SUN3_SCSI, PAGE_SIZE);
  191. sun3_scsi_regp = (unsigned char *)ioaddr;
  192. dregs = (struct sun3_dma_regs *)(((unsigned char *)ioaddr) + 8);
  193. if((udc_regs = dvma_malloc(sizeof(struct sun3_udc_regs)))
  194. == NULL) {
  195. printk("SUN3 Scsi couldn't allocate DVMA memory!\n");
  196. return 0;
  197. }
  198. #ifdef OLDDMA
  199. if((dmabuf = dvma_malloc_align(SUN3_DVMA_BUFSIZE, 0x10000)) == NULL) {
  200. printk("SUN3 Scsi couldn't allocate DVMA memory!\n");
  201. return 0;
  202. }
  203. #endif
  204. #ifdef SUPPORT_TAGS
  205. if (setup_use_tagged_queuing < 0)
  206. setup_use_tagged_queuing = USE_TAGGED_QUEUING;
  207. #endif
  208. instance = scsi_register (tpnt, sizeof(struct NCR5380_hostdata));
  209. if(instance == NULL)
  210. return 0;
  211. default_instance = instance;
  212. instance->io_port = (unsigned long) ioaddr;
  213. instance->irq = IRQ_SUN3_SCSI;
  214. NCR5380_init(instance, 0);
  215. instance->n_io_port = 32;
  216. ((struct NCR5380_hostdata *)instance->hostdata)->ctrl = 0;
  217. if (request_irq(instance->irq, scsi_sun3_intr,
  218. 0, "Sun3SCSI-5380", instance)) {
  219. #ifndef REAL_DMA
  220. printk("scsi%d: IRQ%d not free, interrupts disabled\n",
  221. instance->host_no, instance->irq);
  222. instance->irq = SCSI_IRQ_NONE;
  223. #else
  224. printk("scsi%d: IRQ%d not free, bailing out\n",
  225. instance->host_no, instance->irq);
  226. return 0;
  227. #endif
  228. }
  229. printk("scsi%d: Sun3 5380 at port %lX irq", instance->host_no, instance->io_port);
  230. if (instance->irq == SCSI_IRQ_NONE)
  231. printk ("s disabled");
  232. else
  233. printk (" %d", instance->irq);
  234. printk(" options CAN_QUEUE=%d CMD_PER_LUN=%d release=%d",
  235. instance->can_queue, instance->cmd_per_lun,
  236. SUN3SCSI_PUBLIC_RELEASE);
  237. printk("\nscsi%d:", instance->host_no);
  238. NCR5380_print_options(instance);
  239. printk("\n");
  240. dregs->csr = 0;
  241. udelay(SUN3_DMA_DELAY);
  242. dregs->csr = CSR_SCSI | CSR_FIFO | CSR_INTR;
  243. udelay(SUN3_DMA_DELAY);
  244. dregs->fifo_count = 0;
  245. called = 1;
  246. #ifdef RESET_BOOT
  247. sun3_scsi_reset_boot(instance);
  248. #endif
  249. return 1;
  250. }
  251. int sun3scsi_release (struct Scsi_Host *shpnt)
  252. {
  253. if (shpnt->irq != SCSI_IRQ_NONE)
  254. free_irq(shpnt->irq, shpnt);
  255. iounmap((void *)sun3_scsi_regp);
  256. return 0;
  257. }
  258. #ifdef RESET_BOOT
  259. /*
  260. * Our 'bus reset on boot' function
  261. */
  262. static void sun3_scsi_reset_boot(struct Scsi_Host *instance)
  263. {
  264. unsigned long end;
  265. NCR5380_local_declare();
  266. NCR5380_setup(instance);
  267. /*
  268. * Do a SCSI reset to clean up the bus during initialization. No
  269. * messing with the queues, interrupts, or locks necessary here.
  270. */
  271. printk( "Sun3 SCSI: resetting the SCSI bus..." );
  272. /* switch off SCSI IRQ - catch an interrupt without IRQ bit set else */
  273. // sun3_disable_irq( IRQ_SUN3_SCSI );
  274. /* get in phase */
  275. NCR5380_write( TARGET_COMMAND_REG,
  276. PHASE_SR_TO_TCR( NCR5380_read(STATUS_REG) ));
  277. /* assert RST */
  278. NCR5380_write( INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_RST );
  279. /* The min. reset hold time is 25us, so 40us should be enough */
  280. udelay( 50 );
  281. /* reset RST and interrupt */
  282. NCR5380_write( INITIATOR_COMMAND_REG, ICR_BASE );
  283. NCR5380_read( RESET_PARITY_INTERRUPT_REG );
  284. for( end = jiffies + AFTER_RESET_DELAY; time_before(jiffies, end); )
  285. barrier();
  286. /* switch on SCSI IRQ again */
  287. // sun3_enable_irq( IRQ_SUN3_SCSI );
  288. printk( " done\n" );
  289. }
  290. #endif
  291. const char * sun3scsi_info (struct Scsi_Host *spnt) {
  292. return "";
  293. }
  294. // safe bits for the CSR
  295. #define CSR_GOOD 0x060f
  296. static irqreturn_t scsi_sun3_intr(int irq, void *dummy)
  297. {
  298. unsigned short csr = dregs->csr;
  299. int handled = 0;
  300. if(csr & ~CSR_GOOD) {
  301. if(csr & CSR_DMA_BUSERR) {
  302. printk("scsi%d: bus error in dma\n", default_instance->host_no);
  303. }
  304. if(csr & CSR_DMA_CONFLICT) {
  305. printk("scsi%d: dma conflict\n", default_instance->host_no);
  306. }
  307. handled = 1;
  308. }
  309. if(csr & (CSR_SDB_INT | CSR_DMA_INT)) {
  310. NCR5380_intr(irq, dummy);
  311. handled = 1;
  312. }
  313. return IRQ_RETVAL(handled);
  314. }
  315. /*
  316. * Debug stuff - to be called on NMI, or sysrq key. Use at your own risk;
  317. * reentering NCR5380_print_status seems to have ugly side effects
  318. */
  319. /* this doesn't seem to get used at all -- sam */
  320. #if 0
  321. void sun3_sun3_debug (void)
  322. {
  323. unsigned long flags;
  324. NCR5380_local_declare();
  325. if (default_instance) {
  326. local_irq_save(flags);
  327. NCR5380_print_status(default_instance);
  328. local_irq_restore(flags);
  329. }
  330. }
  331. #endif
  332. /* sun3scsi_dma_setup() -- initialize the dma controller for a read/write */
  333. static unsigned long sun3scsi_dma_setup(void *data, unsigned long count, int write_flag)
  334. {
  335. #ifdef OLDDMA
  336. if(write_flag)
  337. memcpy(dmabuf, data, count);
  338. else {
  339. sun3_dma_orig_addr = data;
  340. sun3_dma_orig_count = count;
  341. }
  342. #else
  343. void *addr;
  344. if(sun3_dma_orig_addr != NULL)
  345. dvma_unmap(sun3_dma_orig_addr);
  346. // addr = sun3_dvma_page((unsigned long)data, (unsigned long)dmabuf);
  347. addr = (void *)dvma_map((unsigned long) data, count);
  348. sun3_dma_orig_addr = addr;
  349. sun3_dma_orig_count = count;
  350. #endif
  351. dregs->fifo_count = 0;
  352. sun3_udc_write(UDC_RESET, UDC_CSR);
  353. /* reset fifo */
  354. dregs->csr &= ~CSR_FIFO;
  355. dregs->csr |= CSR_FIFO;
  356. /* set direction */
  357. if(write_flag)
  358. dregs->csr |= CSR_SEND;
  359. else
  360. dregs->csr &= ~CSR_SEND;
  361. /* byte count for fifo */
  362. dregs->fifo_count = count;
  363. sun3_udc_write(UDC_RESET, UDC_CSR);
  364. /* reset fifo */
  365. dregs->csr &= ~CSR_FIFO;
  366. dregs->csr |= CSR_FIFO;
  367. if(dregs->fifo_count != count) {
  368. printk("scsi%d: fifo_mismatch %04x not %04x\n",
  369. default_instance->host_no, dregs->fifo_count,
  370. (unsigned int) count);
  371. NCR5380_print(default_instance);
  372. }
  373. /* setup udc */
  374. #ifdef OLDDMA
  375. udc_regs->addr_hi = ((dvma_vtob(dmabuf) & 0xff0000) >> 8);
  376. udc_regs->addr_lo = (dvma_vtob(dmabuf) & 0xffff);
  377. #else
  378. udc_regs->addr_hi = (((unsigned long)(addr) & 0xff0000) >> 8);
  379. udc_regs->addr_lo = ((unsigned long)(addr) & 0xffff);
  380. #endif
  381. udc_regs->count = count/2; /* count in words */
  382. udc_regs->mode_hi = UDC_MODE_HIWORD;
  383. if(write_flag) {
  384. if(count & 1)
  385. udc_regs->count++;
  386. udc_regs->mode_lo = UDC_MODE_LSEND;
  387. udc_regs->rsel = UDC_RSEL_SEND;
  388. } else {
  389. udc_regs->mode_lo = UDC_MODE_LRECV;
  390. udc_regs->rsel = UDC_RSEL_RECV;
  391. }
  392. /* announce location of regs block */
  393. sun3_udc_write(((dvma_vtob(udc_regs) & 0xff0000) >> 8),
  394. UDC_CHN_HI);
  395. sun3_udc_write((dvma_vtob(udc_regs) & 0xffff), UDC_CHN_LO);
  396. /* set dma master on */
  397. sun3_udc_write(0xd, UDC_MODE);
  398. /* interrupt enable */
  399. sun3_udc_write(UDC_INT_ENABLE, UDC_CSR);
  400. return count;
  401. }
  402. static inline unsigned long sun3scsi_dma_count(struct Scsi_Host *instance)
  403. {
  404. unsigned short resid;
  405. dregs->udc_addr = 0x32;
  406. udelay(SUN3_DMA_DELAY);
  407. resid = dregs->udc_data;
  408. udelay(SUN3_DMA_DELAY);
  409. resid *= 2;
  410. return (unsigned long) resid;
  411. }
  412. static inline unsigned long sun3scsi_dma_residual(struct Scsi_Host *instance)
  413. {
  414. return last_residual;
  415. }
  416. static inline unsigned long sun3scsi_dma_xfer_len(unsigned long wanted,
  417. struct scsi_cmnd *cmd,
  418. int write_flag)
  419. {
  420. if (cmd->request->cmd_type == REQ_TYPE_FS)
  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 struct 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");