sun3_scsi_vme.c 13 KB

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