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