sun3_scsi_vme.c 13 KB

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