pas16.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594
  1. #define PSEUDO_DMA
  2. #define UNSAFE /* Not unsafe for PAS16 -- use it */
  3. #define PDEBUG 0
  4. /*
  5. * This driver adapted from Drew Eckhardt's Trantor T128 driver
  6. *
  7. * Copyright 1993, Drew Eckhardt
  8. * Visionary Computing
  9. * (Unix and Linux consulting and custom programming)
  10. * drew@colorado.edu
  11. * +1 (303) 666-5836
  12. *
  13. * ( Based on T128 - DISTRIBUTION RELEASE 3. )
  14. *
  15. * Modified to work with the Pro Audio Spectrum/Studio 16
  16. * by John Weidman.
  17. *
  18. *
  19. * For more information, please consult
  20. *
  21. * Media Vision
  22. * (510) 770-8600
  23. * (800) 348-7116
  24. */
  25. /*
  26. * The card is detected and initialized in one of several ways :
  27. * 1. Autoprobe (default) - There are many different models of
  28. * the Pro Audio Spectrum/Studio 16, and I only have one of
  29. * them, so this may require a little tweaking. An interrupt
  30. * is triggered to autoprobe for the interrupt line. Note:
  31. * with the newer model boards, the interrupt is set via
  32. * software after reset using the default_irq for the
  33. * current board number.
  34. *
  35. * 2. With command line overrides - pas16=port,irq may be
  36. * used on the LILO command line to override the defaults.
  37. *
  38. * 3. With the PAS16_OVERRIDE compile time define. This is
  39. * specified as an array of address, irq tuples. Ie, for
  40. * one board at the default 0x388 address, IRQ10, I could say
  41. * -DPAS16_OVERRIDE={{0x388, 10}}
  42. * NOTE: Untested.
  43. *
  44. * 4. When included as a module, with arguments passed on the command line:
  45. * pas16_irq=xx the interrupt
  46. * pas16_addr=xx the port
  47. * e.g. "modprobe pas16 pas16_addr=0x388 pas16_irq=5"
  48. *
  49. * Note that if the override methods are used, place holders must
  50. * be specified for other boards in the system.
  51. *
  52. *
  53. * Configuration notes :
  54. * The current driver does not support interrupt sharing with the
  55. * sound portion of the card. If you use the same irq for the
  56. * scsi port and sound you will have problems. Either use
  57. * a different irq for the scsi port or don't use interrupts
  58. * for the scsi port.
  59. *
  60. * If you have problems with your card not being recognized, use
  61. * the LILO command line override. Try to get it recognized without
  62. * interrupts. Ie, for a board at the default 0x388 base port,
  63. * boot: linux pas16=0x388,0
  64. *
  65. * NO_IRQ (0) should be specified for no interrupt,
  66. * IRQ_AUTO (254) to autoprobe for an IRQ line if overridden
  67. * on the command line.
  68. */
  69. #include <linux/module.h>
  70. #include <linux/signal.h>
  71. #include <linux/proc_fs.h>
  72. #include <asm/io.h>
  73. #include <asm/dma.h>
  74. #include <linux/blkdev.h>
  75. #include <linux/delay.h>
  76. #include <linux/interrupt.h>
  77. #include <linux/stat.h>
  78. #include <linux/init.h>
  79. #include <scsi/scsi_host.h>
  80. #include "pas16.h"
  81. #define AUTOPROBE_IRQ
  82. #include "NCR5380.h"
  83. static unsigned short pas16_addr = 0;
  84. static int pas16_irq = 0;
  85. static const int scsi_irq_translate[] =
  86. { 0, 0, 1, 2, 3, 4, 5, 6, 0, 0, 7, 8, 9, 0, 10, 11 };
  87. /* The default_irqs array contains values used to set the irq into the
  88. * board via software (as must be done on newer model boards without
  89. * irq jumpers on the board). The first value in the array will be
  90. * assigned to logical board 0, the next to board 1, etc.
  91. */
  92. static int default_irqs[] __initdata =
  93. { PAS16_DEFAULT_BOARD_1_IRQ,
  94. PAS16_DEFAULT_BOARD_2_IRQ,
  95. PAS16_DEFAULT_BOARD_3_IRQ,
  96. PAS16_DEFAULT_BOARD_4_IRQ
  97. };
  98. static struct override {
  99. unsigned short io_port;
  100. int irq;
  101. } overrides
  102. #ifdef PAS16_OVERRIDE
  103. [] __initdata = PAS16_OVERRIDE;
  104. #else
  105. [4] __initdata = {{0,IRQ_AUTO}, {0,IRQ_AUTO}, {0,IRQ_AUTO},
  106. {0,IRQ_AUTO}};
  107. #endif
  108. #define NO_OVERRIDES ARRAY_SIZE(overrides)
  109. static struct base {
  110. unsigned short io_port;
  111. int noauto;
  112. } bases[] __initdata =
  113. { {PAS16_DEFAULT_BASE_1, 0},
  114. {PAS16_DEFAULT_BASE_2, 0},
  115. {PAS16_DEFAULT_BASE_3, 0},
  116. {PAS16_DEFAULT_BASE_4, 0}
  117. };
  118. #define NO_BASES ARRAY_SIZE(bases)
  119. static const unsigned short pas16_offset[ 8 ] =
  120. {
  121. 0x1c00, /* OUTPUT_DATA_REG */
  122. 0x1c01, /* INITIATOR_COMMAND_REG */
  123. 0x1c02, /* MODE_REG */
  124. 0x1c03, /* TARGET_COMMAND_REG */
  125. 0x3c00, /* STATUS_REG ro, SELECT_ENABLE_REG wo */
  126. 0x3c01, /* BUS_AND_STATUS_REG ro, START_DMA_SEND_REG wo */
  127. 0x3c02, /* INPUT_DATA_REGISTER ro, (N/A on PAS16 ?)
  128. * START_DMA_TARGET_RECEIVE_REG wo
  129. */
  130. 0x3c03, /* RESET_PARITY_INTERRUPT_REG ro,
  131. * START_DMA_INITIATOR_RECEIVE_REG wo
  132. */
  133. };
  134. /*----------------------------------------------------------------*/
  135. /* the following will set the monitor border color (useful to find
  136. where something crashed or gets stuck at */
  137. /* 1 = blue
  138. 2 = green
  139. 3 = cyan
  140. 4 = red
  141. 5 = magenta
  142. 6 = yellow
  143. 7 = white
  144. */
  145. #if 1
  146. #define rtrc(i) {inb(0x3da); outb(0x31, 0x3c0); outb((i), 0x3c0);}
  147. #else
  148. #define rtrc(i) {}
  149. #endif
  150. /*
  151. * Function : enable_board( int board_num, unsigned short port )
  152. *
  153. * Purpose : set address in new model board
  154. *
  155. * Inputs : board_num - logical board number 0-3, port - base address
  156. *
  157. */
  158. static void __init
  159. enable_board( int board_num, unsigned short port )
  160. {
  161. outb( 0xbc + board_num, MASTER_ADDRESS_PTR );
  162. outb( port >> 2, MASTER_ADDRESS_PTR );
  163. }
  164. /*
  165. * Function : init_board( unsigned short port, int irq )
  166. *
  167. * Purpose : Set the board up to handle the SCSI interface
  168. *
  169. * Inputs : port - base address of the board,
  170. * irq - irq to assign to the SCSI port
  171. * force_irq - set it even if it conflicts with sound driver
  172. *
  173. */
  174. static void __init
  175. init_board( unsigned short io_port, int irq, int force_irq )
  176. {
  177. unsigned int tmp;
  178. unsigned int pas_irq_code;
  179. /* Initialize the SCSI part of the board */
  180. outb( 0x30, io_port + P_TIMEOUT_COUNTER_REG ); /* Timeout counter */
  181. outb( 0x01, io_port + P_TIMEOUT_STATUS_REG_OFFSET ); /* Reset TC */
  182. outb( 0x01, io_port + WAIT_STATE ); /* 1 Wait state */
  183. NCR5380_read( RESET_PARITY_INTERRUPT_REG );
  184. /* Set the SCSI interrupt pointer without mucking up the sound
  185. * interrupt pointer in the same byte.
  186. */
  187. pas_irq_code = ( irq < 16 ) ? scsi_irq_translate[irq] : 0;
  188. tmp = inb( io_port + IO_CONFIG_3 );
  189. if( (( tmp & 0x0f ) == pas_irq_code) && pas_irq_code > 0
  190. && !force_irq )
  191. {
  192. printk( "pas16: WARNING: Can't use same irq as sound "
  193. "driver -- interrupts disabled\n" );
  194. /* Set up the drive parameters, disable 5380 interrupts */
  195. outb( 0x4d, io_port + SYS_CONFIG_4 );
  196. }
  197. else
  198. {
  199. tmp = ( tmp & 0x0f ) | ( pas_irq_code << 4 );
  200. outb( tmp, io_port + IO_CONFIG_3 );
  201. /* Set up the drive parameters and enable 5380 interrupts */
  202. outb( 0x6d, io_port + SYS_CONFIG_4 );
  203. }
  204. }
  205. /*
  206. * Function : pas16_hw_detect( unsigned short board_num )
  207. *
  208. * Purpose : determine if a pas16 board is present
  209. *
  210. * Inputs : board_num - logical board number ( 0 - 3 )
  211. *
  212. * Returns : 0 if board not found, 1 if found.
  213. */
  214. static int __init
  215. pas16_hw_detect( unsigned short board_num )
  216. {
  217. unsigned char board_rev, tmp;
  218. unsigned short io_port = bases[ board_num ].io_port;
  219. /* See if we can find a PAS16 board at the address associated
  220. * with this logical board number.
  221. */
  222. /* First, attempt to take a newer model board out of reset and
  223. * give it a base address. This shouldn't affect older boards.
  224. */
  225. enable_board( board_num, io_port );
  226. /* Now see if it looks like a PAS16 board */
  227. board_rev = inb( io_port + PCB_CONFIG );
  228. if( board_rev == 0xff )
  229. return 0;
  230. tmp = board_rev ^ 0xe0;
  231. outb( tmp, io_port + PCB_CONFIG );
  232. tmp = inb( io_port + PCB_CONFIG );
  233. outb( board_rev, io_port + PCB_CONFIG );
  234. if( board_rev != tmp ) /* Not a PAS-16 */
  235. return 0;
  236. if( ( inb( io_port + OPERATION_MODE_1 ) & 0x03 ) != 0x03 )
  237. return 0; /* return if no SCSI interface found */
  238. /* Mediavision has some new model boards that return ID bits
  239. * that indicate a SCSI interface, but they're not (LMS). We'll
  240. * put in an additional test to try to weed them out.
  241. */
  242. outb( 0x01, io_port + WAIT_STATE ); /* 1 Wait state */
  243. NCR5380_write( MODE_REG, 0x20 ); /* Is it really SCSI? */
  244. if( NCR5380_read( MODE_REG ) != 0x20 ) /* Write to a reg. */
  245. return 0; /* and try to read */
  246. NCR5380_write( MODE_REG, 0x00 ); /* it back. */
  247. if( NCR5380_read( MODE_REG ) != 0x00 )
  248. return 0;
  249. return 1;
  250. }
  251. #ifndef MODULE
  252. /*
  253. * Function : pas16_setup(char *str, int *ints)
  254. *
  255. * Purpose : LILO command line initialization of the overrides array,
  256. *
  257. * Inputs : str - unused, ints - array of integer parameters with ints[0]
  258. * equal to the number of ints.
  259. *
  260. */
  261. static int __init pas16_setup(char *str)
  262. {
  263. static int commandline_current = 0;
  264. int i;
  265. int ints[10];
  266. get_options(str, ARRAY_SIZE(ints), ints);
  267. if (ints[0] != 2)
  268. printk("pas16_setup : usage pas16=io_port,irq\n");
  269. else
  270. if (commandline_current < NO_OVERRIDES) {
  271. overrides[commandline_current].io_port = (unsigned short) ints[1];
  272. overrides[commandline_current].irq = ints[2];
  273. for (i = 0; i < NO_BASES; ++i)
  274. if (bases[i].io_port == (unsigned short) ints[1]) {
  275. bases[i].noauto = 1;
  276. break;
  277. }
  278. ++commandline_current;
  279. }
  280. return 1;
  281. }
  282. __setup("pas16=", pas16_setup);
  283. #endif
  284. /*
  285. * Function : int pas16_detect(struct scsi_host_template * tpnt)
  286. *
  287. * Purpose : detects and initializes PAS16 controllers
  288. * that were autoprobed, overridden on the LILO command line,
  289. * or specified at compile time.
  290. *
  291. * Inputs : tpnt - template for this SCSI adapter.
  292. *
  293. * Returns : 1 if a host adapter was found, 0 if not.
  294. *
  295. */
  296. static int __init pas16_detect(struct scsi_host_template *tpnt)
  297. {
  298. static int current_override = 0;
  299. static unsigned short current_base = 0;
  300. struct Scsi_Host *instance;
  301. unsigned short io_port;
  302. int count;
  303. if (pas16_addr != 0) {
  304. overrides[0].io_port = pas16_addr;
  305. /*
  306. * This is how we avoid seeing more than
  307. * one host adapter at the same I/O port.
  308. * Cribbed shamelessly from pas16_setup().
  309. */
  310. for (count = 0; count < NO_BASES; ++count)
  311. if (bases[count].io_port == pas16_addr) {
  312. bases[count].noauto = 1;
  313. break;
  314. }
  315. }
  316. if (pas16_irq != 0)
  317. overrides[0].irq = pas16_irq;
  318. for (count = 0; current_override < NO_OVERRIDES; ++current_override) {
  319. io_port = 0;
  320. if (overrides[current_override].io_port)
  321. {
  322. io_port = overrides[current_override].io_port;
  323. enable_board( current_override, io_port );
  324. init_board( io_port, overrides[current_override].irq, 1 );
  325. }
  326. else
  327. for (; !io_port && (current_base < NO_BASES); ++current_base) {
  328. #if (PDEBUG & PDEBUG_INIT)
  329. printk("scsi-pas16 : probing io_port %04x\n", (unsigned int) bases[current_base].io_port);
  330. #endif
  331. if ( !bases[current_base].noauto &&
  332. pas16_hw_detect( current_base ) ){
  333. io_port = bases[current_base].io_port;
  334. init_board( io_port, default_irqs[ current_base ], 0 );
  335. #if (PDEBUG & PDEBUG_INIT)
  336. printk("scsi-pas16 : detected board.\n");
  337. #endif
  338. }
  339. }
  340. #if defined(PDEBUG) && (PDEBUG & PDEBUG_INIT)
  341. printk("scsi-pas16 : io_port = %04x\n", (unsigned int) io_port);
  342. #endif
  343. if (!io_port)
  344. break;
  345. instance = scsi_register (tpnt, sizeof(struct NCR5380_hostdata));
  346. if(instance == NULL)
  347. break;
  348. instance->io_port = io_port;
  349. NCR5380_init(instance, 0);
  350. if (overrides[current_override].irq != IRQ_AUTO)
  351. instance->irq = overrides[current_override].irq;
  352. else
  353. instance->irq = NCR5380_probe_irq(instance, PAS16_IRQS);
  354. /* Compatibility with documented NCR5380 kernel parameters */
  355. if (instance->irq == 255)
  356. instance->irq = NO_IRQ;
  357. if (instance->irq != NO_IRQ)
  358. if (request_irq(instance->irq, pas16_intr, 0,
  359. "pas16", instance)) {
  360. printk("scsi%d : IRQ%d not free, interrupts disabled\n",
  361. instance->host_no, instance->irq);
  362. instance->irq = NO_IRQ;
  363. }
  364. if (instance->irq == NO_IRQ) {
  365. printk("scsi%d : interrupts not enabled. for better interactive performance,\n", instance->host_no);
  366. printk("scsi%d : please jumper the board for a free IRQ.\n", instance->host_no);
  367. /* Disable 5380 interrupts, leave drive params the same */
  368. outb( 0x4d, io_port + SYS_CONFIG_4 );
  369. outb( (inb(io_port + IO_CONFIG_3) & 0x0f), io_port + IO_CONFIG_3 );
  370. }
  371. #if defined(PDEBUG) && (PDEBUG & PDEBUG_INIT)
  372. printk("scsi%d : irq = %d\n", instance->host_no, instance->irq);
  373. #endif
  374. ++current_override;
  375. ++count;
  376. }
  377. return count;
  378. }
  379. /*
  380. * Function : int pas16_biosparam(Disk *disk, struct block_device *dev, int *ip)
  381. *
  382. * Purpose : Generates a BIOS / DOS compatible H-C-S mapping for
  383. * the specified device / size.
  384. *
  385. * Inputs : size = size of device in sectors (512 bytes), dev = block device
  386. * major / minor, ip[] = {heads, sectors, cylinders}
  387. *
  388. * Returns : always 0 (success), initializes ip
  389. *
  390. */
  391. /*
  392. * XXX Most SCSI boards use this mapping, I could be incorrect. Some one
  393. * using hard disks on a trantor should verify that this mapping corresponds
  394. * to that used by the BIOS / ASPI driver by running the linux fdisk program
  395. * and matching the H_C_S coordinates to what DOS uses.
  396. */
  397. static int pas16_biosparam(struct scsi_device *sdev, struct block_device *dev,
  398. sector_t capacity, int *ip)
  399. {
  400. int size = capacity;
  401. ip[0] = 64;
  402. ip[1] = 32;
  403. ip[2] = size >> 11; /* I think I have it as /(32*64) */
  404. if( ip[2] > 1024 ) { /* yes, >, not >= */
  405. ip[0]=255;
  406. ip[1]=63;
  407. ip[2]=size/(63*255);
  408. if( ip[2] > 1023 ) /* yes >1023... */
  409. ip[2] = 1023;
  410. }
  411. return 0;
  412. }
  413. /*
  414. * Function : int NCR5380_pread (struct Scsi_Host *instance,
  415. * unsigned char *dst, int len)
  416. *
  417. * Purpose : Fast 5380 pseudo-dma read function, transfers len bytes to
  418. * dst
  419. *
  420. * Inputs : dst = destination, len = length in bytes
  421. *
  422. * Returns : 0 on success, non zero on a failure such as a watchdog
  423. * timeout.
  424. */
  425. static inline int NCR5380_pread (struct Scsi_Host *instance, unsigned char *dst,
  426. int len) {
  427. register unsigned char *d = dst;
  428. register unsigned short reg = (unsigned short) (instance->io_port +
  429. P_DATA_REG_OFFSET);
  430. register int i = len;
  431. int ii = 0;
  432. struct NCR5380_hostdata *hostdata = shost_priv(instance);
  433. while ( !(inb(instance->io_port + P_STATUS_REG_OFFSET) & P_ST_RDY) )
  434. ++ii;
  435. insb( reg, d, i );
  436. if ( inb(instance->io_port + P_TIMEOUT_STATUS_REG_OFFSET) & P_TS_TIM) {
  437. outb( P_TS_CT, instance->io_port + P_TIMEOUT_STATUS_REG_OFFSET);
  438. printk("scsi%d : watchdog timer fired in NCR5380_pread()\n",
  439. instance->host_no);
  440. return -1;
  441. }
  442. if (ii > hostdata->spin_max_r)
  443. hostdata->spin_max_r = ii;
  444. return 0;
  445. }
  446. /*
  447. * Function : int NCR5380_pwrite (struct Scsi_Host *instance,
  448. * unsigned char *src, int len)
  449. *
  450. * Purpose : Fast 5380 pseudo-dma write function, transfers len bytes from
  451. * src
  452. *
  453. * Inputs : src = source, len = length in bytes
  454. *
  455. * Returns : 0 on success, non zero on a failure such as a watchdog
  456. * timeout.
  457. */
  458. static inline int NCR5380_pwrite (struct Scsi_Host *instance, unsigned char *src,
  459. int len) {
  460. register unsigned char *s = src;
  461. register unsigned short reg = (instance->io_port + P_DATA_REG_OFFSET);
  462. register int i = len;
  463. int ii = 0;
  464. struct NCR5380_hostdata *hostdata = shost_priv(instance);
  465. while ( !((inb(instance->io_port + P_STATUS_REG_OFFSET)) & P_ST_RDY) )
  466. ++ii;
  467. outsb( reg, s, i );
  468. if (inb(instance->io_port + P_TIMEOUT_STATUS_REG_OFFSET) & P_TS_TIM) {
  469. outb( P_TS_CT, instance->io_port + P_TIMEOUT_STATUS_REG_OFFSET);
  470. printk("scsi%d : watchdog timer fired in NCR5380_pwrite()\n",
  471. instance->host_no);
  472. return -1;
  473. }
  474. if (ii > hostdata->spin_max_w)
  475. hostdata->spin_max_w = ii;
  476. return 0;
  477. }
  478. #include "NCR5380.c"
  479. static int pas16_release(struct Scsi_Host *shost)
  480. {
  481. if (shost->irq != NO_IRQ)
  482. free_irq(shost->irq, shost);
  483. NCR5380_exit(shost);
  484. if (shost->io_port && shost->n_io_port)
  485. release_region(shost->io_port, shost->n_io_port);
  486. scsi_unregister(shost);
  487. return 0;
  488. }
  489. static struct scsi_host_template driver_template = {
  490. .name = "Pro Audio Spectrum-16 SCSI",
  491. .detect = pas16_detect,
  492. .release = pas16_release,
  493. .proc_name = "pas16",
  494. .show_info = pas16_show_info,
  495. .write_info = pas16_write_info,
  496. .info = pas16_info,
  497. .queuecommand = pas16_queue_command,
  498. .eh_abort_handler = pas16_abort,
  499. .eh_bus_reset_handler = pas16_bus_reset,
  500. .bios_param = pas16_biosparam,
  501. .can_queue = CAN_QUEUE,
  502. .this_id = 7,
  503. .sg_tablesize = SG_ALL,
  504. .cmd_per_lun = CMD_PER_LUN,
  505. .use_clustering = DISABLE_CLUSTERING,
  506. };
  507. #include "scsi_module.c"
  508. #ifdef MODULE
  509. module_param(pas16_addr, ushort, 0);
  510. module_param(pas16_irq, int, 0);
  511. #endif
  512. MODULE_LICENSE("GPL");