sun3_scsi.c 14 KB

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