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