sun3_scsi_vme.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584
  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. * VME support added by Sam Creasey
  7. *
  8. * Adapted from sun3_scsi.c -- see there for other headers
  9. *
  10. * TODO: modify this driver to support multiple Sun3 SCSI VME boards
  11. *
  12. */
  13. #define AUTOSENSE
  14. #include <linux/types.h>
  15. #include <linux/stddef.h>
  16. #include <linux/ctype.h>
  17. #include <linux/delay.h>
  18. #include <linux/module.h>
  19. #include <linux/signal.h>
  20. #include <linux/sched.h>
  21. #include <linux/ioport.h>
  22. #include <linux/init.h>
  23. #include <linux/blkdev.h>
  24. #include <asm/io.h>
  25. #include <asm/system.h>
  26. #include <asm/sun3ints.h>
  27. #include <asm/dvma.h>
  28. #include <asm/idprom.h>
  29. #include <asm/machines.h>
  30. #define SUN3_SCSI_VME
  31. #undef SUN3_SCSI_DEBUG
  32. /* dma on! */
  33. #define REAL_DMA
  34. #include "scsi.h"
  35. #include <scsi/scsi_host.h>
  36. #include "sun3_scsi.h"
  37. #include "NCR5380.h"
  38. extern int sun3_map_test(unsigned long, char *);
  39. #define USE_WRAPPER
  40. /*#define RESET_BOOT */
  41. #define DRIVER_SETUP
  42. #define NDEBUG 0
  43. /*
  44. * BUG can be used to trigger a strange code-size related hang on 2.1 kernels
  45. */
  46. #ifdef BUG
  47. #undef RESET_BOOT
  48. #undef DRIVER_SETUP
  49. #endif
  50. /* #define SUPPORT_TAGS */
  51. //#define ENABLE_IRQ() enable_irq( SUN3_VEC_VMESCSI0 );
  52. #define ENABLE_IRQ()
  53. static irqreturn_t scsi_sun3_intr(int irq, void *dummy, struct pt_regs *fp);
  54. static inline unsigned char sun3scsi_read(int reg);
  55. static inline void sun3scsi_write(int reg, int value);
  56. static int setup_can_queue = -1;
  57. module_param(setup_can_queue, int, 0);
  58. static int setup_cmd_per_lun = -1;
  59. module_param(setup_cmd_per_lun, int, 0);
  60. static int setup_sg_tablesize = -1;
  61. module_param(setup_sg_tablesize, int, 0);
  62. #ifdef SUPPORT_TAGS
  63. static int setup_use_tagged_queuing = -1;
  64. module_param(setup_use_tagged_queuing, int, 0);
  65. #endif
  66. static int setup_hostid = -1;
  67. module_param(setup_hostid, int, 0);
  68. static Scsi_Cmnd *sun3_dma_setup_done = NULL;
  69. #define AFTER_RESET_DELAY (HZ/2)
  70. /* ms to wait after hitting dma regs */
  71. #define SUN3_DMA_DELAY 10
  72. /* dvma buffer to allocate -- 32k should hopefully be more than sufficient */
  73. #define SUN3_DVMA_BUFSIZE 0xe000
  74. /* minimum number of bytes to do dma on */
  75. #define SUN3_DMA_MINSIZE 128
  76. static volatile unsigned char *sun3_scsi_regp;
  77. static volatile struct sun3_dma_regs *dregs;
  78. #ifdef OLDDMA
  79. static unsigned char *dmabuf = NULL; /* dma memory buffer */
  80. #endif
  81. static unsigned char *sun3_dma_orig_addr = NULL;
  82. static unsigned long sun3_dma_orig_count = 0;
  83. static int sun3_dma_active = 0;
  84. static unsigned long last_residual = 0;
  85. /*
  86. * NCR 5380 register access functions
  87. */
  88. static inline unsigned char sun3scsi_read(int reg)
  89. {
  90. return( sun3_scsi_regp[reg] );
  91. }
  92. static inline void sun3scsi_write(int reg, int value)
  93. {
  94. sun3_scsi_regp[reg] = value;
  95. }
  96. /*
  97. * XXX: status debug
  98. */
  99. static struct Scsi_Host *default_instance;
  100. /*
  101. * Function : int sun3scsi_detect(struct scsi_host_template * tpnt)
  102. *
  103. * Purpose : initializes mac NCR5380 driver based on the
  104. * command line / compile time port and irq definitions.
  105. *
  106. * Inputs : tpnt - template for this SCSI adapter.
  107. *
  108. * Returns : 1 if a host adapter was found, 0 if not.
  109. *
  110. */
  111. static int sun3scsi_detect(struct scsi_host_template * tpnt)
  112. {
  113. unsigned long ioaddr, irq = 0;
  114. static int called = 0;
  115. struct Scsi_Host *instance;
  116. int i;
  117. unsigned long addrs[3] = { IOBASE_SUN3_VMESCSI,
  118. IOBASE_SUN3_VMESCSI + 0x4000,
  119. 0 };
  120. unsigned long vecs[3] = { SUN3_VEC_VMESCSI0,
  121. SUN3_VEC_VMESCSI1,
  122. 0 };
  123. /* check that this machine has an onboard 5380 */
  124. switch(idprom->id_machtype) {
  125. case SM_SUN3|SM_3_160:
  126. case SM_SUN3|SM_3_260:
  127. break;
  128. default:
  129. return 0;
  130. }
  131. if(called)
  132. return 0;
  133. tpnt->proc_name = "Sun3 5380 VME SCSI";
  134. /* setup variables */
  135. tpnt->can_queue =
  136. (setup_can_queue > 0) ? setup_can_queue : CAN_QUEUE;
  137. tpnt->cmd_per_lun =
  138. (setup_cmd_per_lun > 0) ? setup_cmd_per_lun : CMD_PER_LUN;
  139. tpnt->sg_tablesize =
  140. (setup_sg_tablesize >= 0) ? setup_sg_tablesize : SG_TABLESIZE;
  141. if (setup_hostid >= 0)
  142. tpnt->this_id = setup_hostid;
  143. else {
  144. /* use 7 as default */
  145. tpnt->this_id = 7;
  146. }
  147. ioaddr = 0;
  148. for(i = 0; addrs[i] != 0; i++) {
  149. unsigned char x;
  150. ioaddr = (unsigned long)sun3_ioremap(addrs[i], PAGE_SIZE,
  151. SUN3_PAGE_TYPE_VME16);
  152. irq = vecs[i];
  153. sun3_scsi_regp = (unsigned char *)ioaddr;
  154. dregs = (struct sun3_dma_regs *)(((unsigned char *)ioaddr) + 8);
  155. if(sun3_map_test((unsigned long)dregs, &x)) {
  156. unsigned short oldcsr;
  157. oldcsr = dregs->csr;
  158. dregs->csr = 0;
  159. udelay(SUN3_DMA_DELAY);
  160. if(dregs->csr == 0x1400)
  161. break;
  162. dregs->csr = oldcsr;
  163. }
  164. iounmap((void *)ioaddr);
  165. ioaddr = 0;
  166. }
  167. if(!ioaddr)
  168. return 0;
  169. #ifdef SUPPORT_TAGS
  170. if (setup_use_tagged_queuing < 0)
  171. setup_use_tagged_queuing = USE_TAGGED_QUEUING;
  172. #endif
  173. instance = scsi_register (tpnt, sizeof(struct NCR5380_hostdata));
  174. if(instance == NULL)
  175. return 0;
  176. default_instance = instance;
  177. instance->io_port = (unsigned long) ioaddr;
  178. instance->irq = irq;
  179. NCR5380_init(instance, 0);
  180. instance->n_io_port = 32;
  181. ((struct NCR5380_hostdata *)instance->hostdata)->ctrl = 0;
  182. if (request_irq(instance->irq, scsi_sun3_intr,
  183. 0, "Sun3SCSI-5380VME", NULL)) {
  184. #ifndef REAL_DMA
  185. printk("scsi%d: IRQ%d not free, interrupts disabled\n",
  186. instance->host_no, instance->irq);
  187. instance->irq = SCSI_IRQ_NONE;
  188. #else
  189. printk("scsi%d: IRQ%d not free, bailing out\n",
  190. instance->host_no, instance->irq);
  191. return 0;
  192. #endif
  193. }
  194. printk("scsi%d: Sun3 5380 VME at port %lX irq", instance->host_no, instance->io_port);
  195. if (instance->irq == SCSI_IRQ_NONE)
  196. printk ("s disabled");
  197. else
  198. printk (" %d", instance->irq);
  199. printk(" options CAN_QUEUE=%d CMD_PER_LUN=%d release=%d",
  200. instance->can_queue, instance->cmd_per_lun,
  201. SUN3SCSI_PUBLIC_RELEASE);
  202. printk("\nscsi%d:", instance->host_no);
  203. NCR5380_print_options(instance);
  204. printk("\n");
  205. dregs->csr = 0;
  206. udelay(SUN3_DMA_DELAY);
  207. dregs->csr = CSR_SCSI | CSR_FIFO | CSR_INTR;
  208. udelay(SUN3_DMA_DELAY);
  209. dregs->fifo_count = 0;
  210. dregs->fifo_count_hi = 0;
  211. dregs->dma_addr_hi = 0;
  212. dregs->dma_addr_lo = 0;
  213. dregs->dma_count_hi = 0;
  214. dregs->dma_count_lo = 0;
  215. dregs->ivect = VME_DATA24 | (instance->irq & 0xff);
  216. called = 1;
  217. #ifdef RESET_BOOT
  218. sun3_scsi_reset_boot(instance);
  219. #endif
  220. return 1;
  221. }
  222. int sun3scsi_release (struct Scsi_Host *shpnt)
  223. {
  224. if (shpnt->irq != SCSI_IRQ_NONE)
  225. free_irq (shpnt->irq, NULL);
  226. iounmap((void *)sun3_scsi_regp);
  227. return 0;
  228. }
  229. #ifdef RESET_BOOT
  230. /*
  231. * Our 'bus reset on boot' function
  232. */
  233. static void sun3_scsi_reset_boot(struct Scsi_Host *instance)
  234. {
  235. unsigned long end;
  236. NCR5380_local_declare();
  237. NCR5380_setup(instance);
  238. /*
  239. * Do a SCSI reset to clean up the bus during initialization. No
  240. * messing with the queues, interrupts, or locks necessary here.
  241. */
  242. printk( "Sun3 SCSI: resetting the SCSI bus..." );
  243. /* switch off SCSI IRQ - catch an interrupt without IRQ bit set else */
  244. // sun3_disable_irq( IRQ_SUN3_SCSI );
  245. /* get in phase */
  246. NCR5380_write( TARGET_COMMAND_REG,
  247. PHASE_SR_TO_TCR( NCR5380_read(STATUS_REG) ));
  248. /* assert RST */
  249. NCR5380_write( INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_RST );
  250. /* The min. reset hold time is 25us, so 40us should be enough */
  251. udelay( 50 );
  252. /* reset RST and interrupt */
  253. NCR5380_write( INITIATOR_COMMAND_REG, ICR_BASE );
  254. NCR5380_read( RESET_PARITY_INTERRUPT_REG );
  255. for( end = jiffies + AFTER_RESET_DELAY; time_before(jiffies, end); )
  256. barrier();
  257. /* switch on SCSI IRQ again */
  258. // sun3_enable_irq( IRQ_SUN3_SCSI );
  259. printk( " done\n" );
  260. }
  261. #endif
  262. static const char * sun3scsi_info (struct Scsi_Host *spnt) {
  263. return "";
  264. }
  265. // safe bits for the CSR
  266. #define CSR_GOOD 0x060f
  267. static irqreturn_t scsi_sun3_intr(int irq, void *dummy, struct pt_regs *fp)
  268. {
  269. unsigned short csr = dregs->csr;
  270. int handled = 0;
  271. dregs->csr &= ~CSR_DMA_ENABLE;
  272. #ifdef SUN3_SCSI_DEBUG
  273. printk("scsi_intr csr %x\n", csr);
  274. #endif
  275. if(csr & ~CSR_GOOD) {
  276. if(csr & CSR_DMA_BUSERR) {
  277. printk("scsi%d: bus error in dma\n", default_instance->host_no);
  278. #ifdef SUN3_SCSI_DEBUG
  279. printk("scsi: residual %x count %x addr %p dmaaddr %x\n",
  280. dregs->fifo_count,
  281. dregs->dma_count_lo | (dregs->dma_count_hi << 16),
  282. sun3_dma_orig_addr,
  283. dregs->dma_addr_lo | (dregs->dma_addr_hi << 16));
  284. #endif
  285. }
  286. if(csr & CSR_DMA_CONFLICT) {
  287. printk("scsi%d: dma conflict\n", default_instance->host_no);
  288. }
  289. handled = 1;
  290. }
  291. if(csr & (CSR_SDB_INT | CSR_DMA_INT)) {
  292. NCR5380_intr(irq, dummy, fp);
  293. handled = 1;
  294. }
  295. return IRQ_RETVAL(handled);
  296. }
  297. /*
  298. * Debug stuff - to be called on NMI, or sysrq key. Use at your own risk;
  299. * reentering NCR5380_print_status seems to have ugly side effects
  300. */
  301. /* this doesn't seem to get used at all -- sam */
  302. #if 0
  303. void sun3_sun3_debug (void)
  304. {
  305. unsigned long flags;
  306. NCR5380_local_declare();
  307. if (default_instance) {
  308. local_irq_save(flags);
  309. NCR5380_print_status(default_instance);
  310. local_irq_restore(flags);
  311. }
  312. }
  313. #endif
  314. /* sun3scsi_dma_setup() -- initialize the dma controller for a read/write */
  315. static unsigned long sun3scsi_dma_setup(void *data, unsigned long count, int write_flag)
  316. {
  317. void *addr;
  318. if(sun3_dma_orig_addr != NULL)
  319. dvma_unmap(sun3_dma_orig_addr);
  320. // addr = sun3_dvma_page((unsigned long)data, (unsigned long)dmabuf);
  321. addr = (void *)dvma_map_vme((unsigned long) data, count);
  322. sun3_dma_orig_addr = addr;
  323. sun3_dma_orig_count = count;
  324. #ifdef SUN3_SCSI_DEBUG
  325. printk("scsi: dma_setup addr %p count %x\n", addr, count);
  326. #endif
  327. // dregs->fifo_count = 0;
  328. #if 0
  329. /* reset fifo */
  330. dregs->csr &= ~CSR_FIFO;
  331. dregs->csr |= CSR_FIFO;
  332. #endif
  333. /* set direction */
  334. if(write_flag)
  335. dregs->csr |= CSR_SEND;
  336. else
  337. dregs->csr &= ~CSR_SEND;
  338. /* reset fifo */
  339. // dregs->csr &= ~CSR_FIFO;
  340. // dregs->csr |= CSR_FIFO;
  341. dregs->csr |= CSR_PACK_ENABLE;
  342. dregs->dma_addr_hi = ((unsigned long)addr >> 16);
  343. dregs->dma_addr_lo = ((unsigned long)addr & 0xffff);
  344. dregs->dma_count_hi = 0;
  345. dregs->dma_count_lo = 0;
  346. dregs->fifo_count_hi = 0;
  347. dregs->fifo_count = 0;
  348. #ifdef SUN3_SCSI_DEBUG
  349. printk("scsi: dma_setup done csr %x\n", dregs->csr);
  350. #endif
  351. return count;
  352. }
  353. static inline unsigned long sun3scsi_dma_residual(struct Scsi_Host *instance)
  354. {
  355. return last_residual;
  356. }
  357. static inline unsigned long sun3scsi_dma_xfer_len(unsigned long wanted, Scsi_Cmnd *cmd,
  358. int write_flag)
  359. {
  360. if(cmd->request->flags & REQ_CMD)
  361. return wanted;
  362. else
  363. return 0;
  364. }
  365. static int sun3scsi_dma_start(unsigned long count, char *data)
  366. {
  367. unsigned short csr;
  368. csr = dregs->csr;
  369. #ifdef SUN3_SCSI_DEBUG
  370. printk("scsi: dma_start data %p count %x csr %x fifo %x\n", data, count, csr, dregs->fifo_count);
  371. #endif
  372. dregs->dma_count_hi = (sun3_dma_orig_count >> 16);
  373. dregs->dma_count_lo = (sun3_dma_orig_count & 0xffff);
  374. dregs->fifo_count_hi = (sun3_dma_orig_count >> 16);
  375. dregs->fifo_count = (sun3_dma_orig_count & 0xffff);
  376. // if(!(csr & CSR_DMA_ENABLE))
  377. // dregs->csr |= CSR_DMA_ENABLE;
  378. return 0;
  379. }
  380. /* clean up after our dma is done */
  381. static int sun3scsi_dma_finish(int write_flag)
  382. {
  383. unsigned short fifo;
  384. int ret = 0;
  385. sun3_dma_active = 0;
  386. dregs->csr &= ~CSR_DMA_ENABLE;
  387. fifo = dregs->fifo_count;
  388. if(write_flag) {
  389. if((fifo > 0) && (fifo < sun3_dma_orig_count))
  390. fifo++;
  391. }
  392. last_residual = fifo;
  393. #ifdef SUN3_SCSI_DEBUG
  394. printk("scsi: residual %x total %x\n", fifo, sun3_dma_orig_count);
  395. #endif
  396. /* empty bytes from the fifo which didn't make it */
  397. if((!write_flag) && (dregs->csr & CSR_LEFT)) {
  398. unsigned char *vaddr;
  399. #ifdef SUN3_SCSI_DEBUG
  400. printk("scsi: got left over bytes\n");
  401. #endif
  402. vaddr = (unsigned char *)dvma_vmetov(sun3_dma_orig_addr);
  403. vaddr += (sun3_dma_orig_count - fifo);
  404. vaddr--;
  405. switch(dregs->csr & CSR_LEFT) {
  406. case CSR_LEFT_3:
  407. *vaddr = (dregs->bpack_lo & 0xff00) >> 8;
  408. vaddr--;
  409. case CSR_LEFT_2:
  410. *vaddr = (dregs->bpack_hi & 0x00ff);
  411. vaddr--;
  412. case CSR_LEFT_1:
  413. *vaddr = (dregs->bpack_hi & 0xff00) >> 8;
  414. break;
  415. }
  416. }
  417. dvma_unmap(sun3_dma_orig_addr);
  418. sun3_dma_orig_addr = NULL;
  419. dregs->dma_addr_hi = 0;
  420. dregs->dma_addr_lo = 0;
  421. dregs->dma_count_hi = 0;
  422. dregs->dma_count_lo = 0;
  423. dregs->fifo_count = 0;
  424. dregs->fifo_count_hi = 0;
  425. dregs->csr &= ~CSR_SEND;
  426. // dregs->csr |= CSR_DMA_ENABLE;
  427. #if 0
  428. /* reset fifo */
  429. dregs->csr &= ~CSR_FIFO;
  430. dregs->csr |= CSR_FIFO;
  431. #endif
  432. sun3_dma_setup_done = NULL;
  433. return ret;
  434. }
  435. #include "sun3_NCR5380.c"
  436. static struct scsi_host_template driver_template = {
  437. .name = SUN3_SCSI_NAME,
  438. .detect = sun3scsi_detect,
  439. .release = sun3scsi_release,
  440. .info = sun3scsi_info,
  441. .queuecommand = sun3scsi_queue_command,
  442. .eh_abort_handler = sun3scsi_abort,
  443. .eh_bus_reset_handler = sun3scsi_bus_reset,
  444. .can_queue = CAN_QUEUE,
  445. .this_id = 7,
  446. .sg_tablesize = SG_TABLESIZE,
  447. .cmd_per_lun = CMD_PER_LUN,
  448. .use_clustering = DISABLE_CLUSTERING
  449. };
  450. #include "scsi_module.c"