sym53c416.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866
  1. /*
  2. * sym53c416.c
  3. * Low-level SCSI driver for sym53c416 chip.
  4. * Copyright (C) 1998 Lieven Willems (lw_linux@hotmail.com)
  5. *
  6. * Changes :
  7. *
  8. * Marcelo Tosatti <marcelo@conectiva.com.br> : Added io_request_lock locking
  9. * Alan Cox <alan@redhat.com> : Cleaned up code formatting
  10. * Fixed an irq locking bug
  11. * Added ISAPnP support
  12. * Bjoern A. Zeeb <bzeeb@zabbadoz.net> : Initial irq locking updates
  13. * Added another card with ISAPnP support
  14. *
  15. * LILO command line usage: sym53c416=<PORTBASE>[,<IRQ>]
  16. *
  17. * This program is free software; you can redistribute it and/or modify it
  18. * under the terms of the GNU General Public License as published by the
  19. * Free Software Foundation; either version 2, or (at your option) any
  20. * later version.
  21. *
  22. * This program is distributed in the hope that it will be useful, but
  23. * WITHOUT ANY WARRANTY; without even the implied warranty of
  24. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  25. * General Public License for more details.
  26. *
  27. */
  28. #include <linux/module.h>
  29. #include <linux/kernel.h>
  30. #include <linux/types.h>
  31. #include <linux/init.h>
  32. #include <linux/string.h>
  33. #include <linux/ioport.h>
  34. #include <linux/interrupt.h>
  35. #include <linux/delay.h>
  36. #include <linux/proc_fs.h>
  37. #include <linux/spinlock.h>
  38. #include <asm/dma.h>
  39. #include <asm/system.h>
  40. #include <asm/io.h>
  41. #include <linux/blkdev.h>
  42. #include <linux/isapnp.h>
  43. #include "scsi.h"
  44. #include <scsi/scsi_host.h>
  45. #include "sym53c416.h"
  46. #define VERSION_STRING "Version 1.0.0-ac"
  47. #define TC_LOW 0x00 /* Transfer counter low */
  48. #define TC_MID 0x01 /* Transfer counter mid */
  49. #define SCSI_FIFO 0x02 /* SCSI FIFO register */
  50. #define COMMAND_REG 0x03 /* Command Register */
  51. #define STATUS_REG 0x04 /* Status Register (READ) */
  52. #define DEST_BUS_ID 0x04 /* Destination Bus ID (WRITE) */
  53. #define INT_REG 0x05 /* Interrupt Register (READ) */
  54. #define TOM 0x05 /* Time out multiplier (WRITE) */
  55. #define STP 0x06 /* Synchronous Transfer period */
  56. #define SYNC_OFFSET 0x07 /* Synchronous Offset */
  57. #define CONF_REG_1 0x08 /* Configuration register 1 */
  58. #define CONF_REG_2 0x0B /* Configuration register 2 */
  59. #define CONF_REG_3 0x0C /* Configuration register 3 */
  60. #define CONF_REG_4 0x0D /* Configuration register 4 */
  61. #define TC_HIGH 0x0E /* Transfer counter high */
  62. #define PIO_FIFO_1 0x10 /* PIO FIFO register 1 */
  63. #define PIO_FIFO_2 0x11 /* PIO FIFO register 2 */
  64. #define PIO_FIFO_3 0x12 /* PIO FIFO register 3 */
  65. #define PIO_FIFO_4 0x13 /* PIO FIFO register 4 */
  66. #define PIO_FIFO_CNT 0x14 /* PIO FIFO count */
  67. #define PIO_INT_REG 0x15 /* PIO interrupt register */
  68. #define CONF_REG_5 0x16 /* Configuration register 5 */
  69. #define FEATURE_EN 0x1D /* Feature Enable register */
  70. /* Configuration register 1 entries: */
  71. /* Bits 2-0: SCSI ID of host adapter */
  72. #define SCM 0x80 /* Slow Cable Mode */
  73. #define SRID 0x40 /* SCSI Reset Interrupt Disable */
  74. #define PTM 0x20 /* Parity Test Mode */
  75. #define EPC 0x10 /* Enable Parity Checking */
  76. #define CTME 0x08 /* Special Test Mode */
  77. /* Configuration register 2 entries: */
  78. #define FE 0x40 /* Features Enable */
  79. #define SCSI2 0x08 /* SCSI 2 Enable */
  80. #define TBPA 0x04 /* Target Bad Parity Abort */
  81. /* Configuration register 3 entries: */
  82. #define IDMRC 0x80 /* ID Message Reserved Check */
  83. #define QTE 0x40 /* Queue Tag Enable */
  84. #define CDB10 0x20 /* Command Descriptor Block 10 */
  85. #define FSCSI 0x10 /* FastSCSI */
  86. #define FCLK 0x08 /* FastClock */
  87. /* Configuration register 4 entries: */
  88. #define RBS 0x08 /* Register bank select */
  89. #define EAN 0x04 /* Enable Active Negotiation */
  90. /* Configuration register 5 entries: */
  91. #define LPSR 0x80 /* Lower Power SCSI Reset */
  92. #define IE 0x20 /* Interrupt Enable */
  93. #define LPM 0x02 /* Low Power Mode */
  94. #define WSE0 0x01 /* 0WS Enable */
  95. /* Interrupt register entries: */
  96. #define SRST 0x80 /* SCSI Reset */
  97. #define ILCMD 0x40 /* Illegal Command */
  98. #define DIS 0x20 /* Disconnect */
  99. #define BS 0x10 /* Bus Service */
  100. #define FC 0x08 /* Function Complete */
  101. #define RESEL 0x04 /* Reselected */
  102. #define SI 0x03 /* Selection Interrupt */
  103. /* Status Register Entries: */
  104. #define SCI 0x80 /* SCSI Core Int */
  105. #define GE 0x40 /* Gross Error */
  106. #define PE 0x20 /* Parity Error */
  107. #define TC 0x10 /* Terminal Count */
  108. #define VGC 0x08 /* Valid Group Code */
  109. #define PHBITS 0x07 /* Phase bits */
  110. /* PIO Interrupt Register Entries: */
  111. #define SCI 0x80 /* SCSI Core Int */
  112. #define PFI 0x40 /* PIO FIFO Interrupt */
  113. #define FULL 0x20 /* PIO FIFO Full */
  114. #define EMPTY 0x10 /* PIO FIFO Empty */
  115. #define CE 0x08 /* Collision Error */
  116. #define OUE 0x04 /* Overflow / Underflow error */
  117. #define FIE 0x02 /* Full Interrupt Enable */
  118. #define EIE 0x01 /* Empty Interrupt Enable */
  119. /* SYM53C416 SCSI phases (lower 3 bits of SYM53C416_STATUS_REG) */
  120. #define PHASE_DATA_OUT 0x00
  121. #define PHASE_DATA_IN 0x01
  122. #define PHASE_COMMAND 0x02
  123. #define PHASE_STATUS 0x03
  124. #define PHASE_RESERVED_1 0x04
  125. #define PHASE_RESERVED_2 0x05
  126. #define PHASE_MESSAGE_OUT 0x06
  127. #define PHASE_MESSAGE_IN 0x07
  128. /* SYM53C416 core commands */
  129. #define NOOP 0x00
  130. #define FLUSH_FIFO 0x01
  131. #define RESET_CHIP 0x02
  132. #define RESET_SCSI_BUS 0x03
  133. #define DISABLE_SEL_RESEL 0x45
  134. #define RESEL_SEQ 0x40
  135. #define SEL_WITHOUT_ATN_SEQ 0x41
  136. #define SEL_WITH_ATN_SEQ 0x42
  137. #define SEL_WITH_ATN_AND_STOP_SEQ 0x43
  138. #define ENABLE_SEL_RESEL 0x44
  139. #define SEL_WITH_ATN3_SEQ 0x46
  140. #define RESEL3_SEQ 0x47
  141. #define SND_MSG 0x20
  142. #define SND_STAT 0x21
  143. #define SND_DATA 0x22
  144. #define DISCONNECT_SEQ 0x23
  145. #define TERMINATE_SEQ 0x24
  146. #define TARGET_COMM_COMPLETE_SEQ 0x25
  147. #define DISCONN 0x27
  148. #define RECV_MSG_SEQ 0x28
  149. #define RECV_CMD 0x29
  150. #define RECV_DATA 0x2A
  151. #define RECV_CMD_SEQ 0x2B
  152. #define TARGET_ABORT_PIO 0x04
  153. #define TRANSFER_INFORMATION 0x10
  154. #define INIT_COMM_COMPLETE_SEQ 0x11
  155. #define MSG_ACCEPTED 0x12
  156. #define TRANSFER_PAD 0x18
  157. #define SET_ATN 0x1A
  158. #define RESET_ATN 0x1B
  159. #define ILLEGAL 0xFF
  160. #define PIO_MODE 0x80
  161. #define IO_RANGE 0x20 /* 0x00 - 0x1F */
  162. #define ID "sym53c416" /* Attention: copied to the sym53c416.h */
  163. #define PIO_SIZE 128 /* Size of PIO fifo is 128 bytes */
  164. #define READ_TIMEOUT 150
  165. #define WRITE_TIMEOUT 150
  166. #ifdef MODULE
  167. #define sym53c416_base sym53c416
  168. #define sym53c416_base_1 sym53c416_1
  169. #define sym53c416_base_2 sym53c416_2
  170. #define sym53c416_base_3 sym53c416_3
  171. static unsigned int sym53c416_base[2] = {0,0};
  172. static unsigned int sym53c416_base_1[2] = {0,0};
  173. static unsigned int sym53c416_base_2[2] = {0,0};
  174. static unsigned int sym53c416_base_3[2] = {0,0};
  175. #endif
  176. #define MAXHOSTS 4
  177. #define SG_ADDRESS(buffer) ((char *) (page_address((buffer)->page)+(buffer)->offset))
  178. enum phases
  179. {
  180. idle,
  181. data_out,
  182. data_in,
  183. command_ph,
  184. status_ph,
  185. message_out,
  186. message_in
  187. };
  188. typedef struct
  189. {
  190. int base;
  191. int irq;
  192. int scsi_id;
  193. } host;
  194. static host hosts[MAXHOSTS] = {
  195. {0, 0, SYM53C416_SCSI_ID},
  196. {0, 0, SYM53C416_SCSI_ID},
  197. {0, 0, SYM53C416_SCSI_ID},
  198. {0, 0, SYM53C416_SCSI_ID}
  199. };
  200. static int host_index = 0;
  201. static char info[120];
  202. static Scsi_Cmnd *current_command = NULL;
  203. static int fastpio = 1;
  204. static int probeaddrs[] = {0x200, 0x220, 0x240, 0};
  205. static void sym53c416_set_transfer_counter(int base, unsigned int len)
  206. {
  207. /* Program Transfer Counter */
  208. outb(len & 0x0000FF, base + TC_LOW);
  209. outb((len & 0x00FF00) >> 8, base + TC_MID);
  210. outb((len & 0xFF0000) >> 16, base + TC_HIGH);
  211. }
  212. static DEFINE_SPINLOCK(sym53c416_lock);
  213. /* Returns the number of bytes read */
  214. static __inline__ unsigned int sym53c416_read(int base, unsigned char *buffer, unsigned int len)
  215. {
  216. unsigned int orig_len = len;
  217. unsigned long flags = 0;
  218. unsigned int bytes_left;
  219. unsigned long i;
  220. int timeout = READ_TIMEOUT;
  221. /* Do transfer */
  222. spin_lock_irqsave(&sym53c416_lock, flags);
  223. while(len && timeout)
  224. {
  225. bytes_left = inb(base + PIO_FIFO_CNT); /* Number of bytes in the PIO FIFO */
  226. if(fastpio && bytes_left > 3)
  227. {
  228. insl(base + PIO_FIFO_1, buffer, bytes_left >> 2);
  229. buffer += bytes_left & 0xFC;
  230. len -= bytes_left & 0xFC;
  231. }
  232. else if(bytes_left > 0)
  233. {
  234. len -= bytes_left;
  235. for(; bytes_left > 0; bytes_left--)
  236. *(buffer++) = inb(base + PIO_FIFO_1);
  237. }
  238. else
  239. {
  240. i = jiffies + timeout;
  241. spin_unlock_irqrestore(&sym53c416_lock, flags);
  242. while(time_before(jiffies, i) && (inb(base + PIO_INT_REG) & EMPTY) && timeout)
  243. if(inb(base + PIO_INT_REG) & SCI)
  244. timeout = 0;
  245. spin_lock_irqsave(&sym53c416_lock, flags);
  246. if(inb(base + PIO_INT_REG) & EMPTY)
  247. timeout = 0;
  248. }
  249. }
  250. spin_unlock_irqrestore(&sym53c416_lock, flags);
  251. return orig_len - len;
  252. }
  253. /* Returns the number of bytes written */
  254. static __inline__ unsigned int sym53c416_write(int base, unsigned char *buffer, unsigned int len)
  255. {
  256. unsigned int orig_len = len;
  257. unsigned long flags = 0;
  258. unsigned int bufferfree;
  259. unsigned long i;
  260. unsigned int timeout = WRITE_TIMEOUT;
  261. /* Do transfer */
  262. spin_lock_irqsave(&sym53c416_lock, flags);
  263. while(len && timeout)
  264. {
  265. bufferfree = PIO_SIZE - inb(base + PIO_FIFO_CNT);
  266. if(bufferfree > len)
  267. bufferfree = len;
  268. if(fastpio && bufferfree > 3)
  269. {
  270. outsl(base + PIO_FIFO_1, buffer, bufferfree >> 2);
  271. buffer += bufferfree & 0xFC;
  272. len -= bufferfree & 0xFC;
  273. }
  274. else if(bufferfree > 0)
  275. {
  276. len -= bufferfree;
  277. for(; bufferfree > 0; bufferfree--)
  278. outb(*(buffer++), base + PIO_FIFO_1);
  279. }
  280. else
  281. {
  282. i = jiffies + timeout;
  283. spin_unlock_irqrestore(&sym53c416_lock, flags);
  284. while(time_before(jiffies, i) && (inb(base + PIO_INT_REG) & FULL) && timeout)
  285. ;
  286. spin_lock_irqsave(&sym53c416_lock, flags);
  287. if(inb(base + PIO_INT_REG) & FULL)
  288. timeout = 0;
  289. }
  290. }
  291. spin_unlock_irqrestore(&sym53c416_lock, flags);
  292. return orig_len - len;
  293. }
  294. static irqreturn_t sym53c416_intr_handle(int irq, void *dev_id)
  295. {
  296. struct Scsi_Host *dev = dev_id;
  297. int base = 0;
  298. int i;
  299. unsigned long flags = 0;
  300. unsigned char status_reg, pio_int_reg, int_reg;
  301. struct scatterlist *sglist;
  302. unsigned int sgcount;
  303. unsigned int tot_trans = 0;
  304. /* We search the base address of the host adapter which caused the interrupt */
  305. /* FIXME: should pass dev_id sensibly as hosts[i] */
  306. for(i = 0; i < host_index && !base; i++)
  307. if(irq == hosts[i].irq)
  308. base = hosts[i].base;
  309. /* If no adapter found, we cannot handle the interrupt. Leave a message */
  310. /* and continue. This should never happen... */
  311. if(!base)
  312. {
  313. printk(KERN_ERR "sym53c416: No host adapter defined for interrupt %d\n", irq);
  314. return IRQ_NONE;
  315. }
  316. /* Now we have the base address and we can start handling the interrupt */
  317. spin_lock_irqsave(dev->host_lock,flags);
  318. status_reg = inb(base + STATUS_REG);
  319. pio_int_reg = inb(base + PIO_INT_REG);
  320. int_reg = inb(base + INT_REG);
  321. spin_unlock_irqrestore(dev->host_lock, flags);
  322. /* First, we handle error conditions */
  323. if(int_reg & SCI) /* SCSI Reset */
  324. {
  325. printk(KERN_DEBUG "sym53c416: Reset received\n");
  326. current_command->SCp.phase = idle;
  327. current_command->result = DID_RESET << 16;
  328. spin_lock_irqsave(dev->host_lock, flags);
  329. current_command->scsi_done(current_command);
  330. spin_unlock_irqrestore(dev->host_lock, flags);
  331. goto out;
  332. }
  333. if(int_reg & ILCMD) /* Illegal Command */
  334. {
  335. printk(KERN_WARNING "sym53c416: Illegal Command: 0x%02x.\n", inb(base + COMMAND_REG));
  336. current_command->SCp.phase = idle;
  337. current_command->result = DID_ERROR << 16;
  338. spin_lock_irqsave(dev->host_lock, flags);
  339. current_command->scsi_done(current_command);
  340. spin_unlock_irqrestore(dev->host_lock, flags);
  341. goto out;
  342. }
  343. if(status_reg & GE) /* Gross Error */
  344. {
  345. printk(KERN_WARNING "sym53c416: Controller reports gross error.\n");
  346. current_command->SCp.phase = idle;
  347. current_command->result = DID_ERROR << 16;
  348. spin_lock_irqsave(dev->host_lock, flags);
  349. current_command->scsi_done(current_command);
  350. spin_unlock_irqrestore(dev->host_lock, flags);
  351. goto out;
  352. }
  353. if(status_reg & PE) /* Parity Error */
  354. {
  355. printk(KERN_WARNING "sym53c416:SCSI parity error.\n");
  356. current_command->SCp.phase = idle;
  357. current_command->result = DID_PARITY << 16;
  358. spin_lock_irqsave(dev->host_lock, flags);
  359. current_command->scsi_done(current_command);
  360. spin_unlock_irqrestore(dev->host_lock, flags);
  361. goto out;
  362. }
  363. if(pio_int_reg & (CE | OUE))
  364. {
  365. printk(KERN_WARNING "sym53c416: PIO interrupt error.\n");
  366. current_command->SCp.phase = idle;
  367. current_command->result = DID_ERROR << 16;
  368. spin_lock_irqsave(dev->host_lock, flags);
  369. current_command->scsi_done(current_command);
  370. spin_unlock_irqrestore(dev->host_lock, flags);
  371. goto out;
  372. }
  373. if(int_reg & DIS) /* Disconnect */
  374. {
  375. if(current_command->SCp.phase != message_in)
  376. current_command->result = DID_NO_CONNECT << 16;
  377. else
  378. current_command->result = (current_command->SCp.Status & 0xFF) | ((current_command->SCp.Message & 0xFF) << 8) | (DID_OK << 16);
  379. current_command->SCp.phase = idle;
  380. spin_lock_irqsave(dev->host_lock, flags);
  381. current_command->scsi_done(current_command);
  382. spin_unlock_irqrestore(dev->host_lock, flags);
  383. goto out;
  384. }
  385. /* Now we handle SCSI phases */
  386. switch(status_reg & PHBITS) /* Filter SCSI phase out of status reg */
  387. {
  388. case PHASE_DATA_OUT:
  389. {
  390. if(int_reg & BS)
  391. {
  392. current_command->SCp.phase = data_out;
  393. outb(FLUSH_FIFO, base + COMMAND_REG);
  394. sym53c416_set_transfer_counter(base, current_command->request_bufflen);
  395. outb(TRANSFER_INFORMATION | PIO_MODE, base + COMMAND_REG);
  396. if(!current_command->use_sg)
  397. tot_trans = sym53c416_write(base, current_command->request_buffer, current_command->request_bufflen);
  398. else
  399. {
  400. sgcount = current_command->use_sg;
  401. sglist = current_command->request_buffer;
  402. while(sgcount--)
  403. {
  404. tot_trans += sym53c416_write(base, SG_ADDRESS(sglist), sglist->length);
  405. sglist++;
  406. }
  407. }
  408. if(tot_trans < current_command->underflow)
  409. printk(KERN_WARNING "sym53c416: Underflow, wrote %d bytes, request for %d bytes.\n", tot_trans, current_command->underflow);
  410. }
  411. break;
  412. }
  413. case PHASE_DATA_IN:
  414. {
  415. if(int_reg & BS)
  416. {
  417. current_command->SCp.phase = data_in;
  418. outb(FLUSH_FIFO, base + COMMAND_REG);
  419. sym53c416_set_transfer_counter(base, current_command->request_bufflen);
  420. outb(TRANSFER_INFORMATION | PIO_MODE, base + COMMAND_REG);
  421. if(!current_command->use_sg)
  422. tot_trans = sym53c416_read(base, current_command->request_buffer, current_command->request_bufflen);
  423. else
  424. {
  425. sgcount = current_command->use_sg;
  426. sglist = current_command->request_buffer;
  427. while(sgcount--)
  428. {
  429. tot_trans += sym53c416_read(base, SG_ADDRESS(sglist), sglist->length);
  430. sglist++;
  431. }
  432. }
  433. if(tot_trans < current_command->underflow)
  434. printk(KERN_WARNING "sym53c416: Underflow, read %d bytes, request for %d bytes.\n", tot_trans, current_command->underflow);
  435. }
  436. break;
  437. }
  438. case PHASE_COMMAND:
  439. {
  440. current_command->SCp.phase = command_ph;
  441. printk(KERN_ERR "sym53c416: Unknown interrupt in command phase.\n");
  442. break;
  443. }
  444. case PHASE_STATUS:
  445. {
  446. current_command->SCp.phase = status_ph;
  447. outb(FLUSH_FIFO, base + COMMAND_REG);
  448. outb(INIT_COMM_COMPLETE_SEQ, base + COMMAND_REG);
  449. break;
  450. }
  451. case PHASE_RESERVED_1:
  452. case PHASE_RESERVED_2:
  453. {
  454. printk(KERN_ERR "sym53c416: Reserved phase occurred.\n");
  455. break;
  456. }
  457. case PHASE_MESSAGE_OUT:
  458. {
  459. current_command->SCp.phase = message_out;
  460. outb(SET_ATN, base + COMMAND_REG);
  461. outb(MSG_ACCEPTED, base + COMMAND_REG);
  462. break;
  463. }
  464. case PHASE_MESSAGE_IN:
  465. {
  466. current_command->SCp.phase = message_in;
  467. current_command->SCp.Status = inb(base + SCSI_FIFO);
  468. current_command->SCp.Message = inb(base + SCSI_FIFO);
  469. if(current_command->SCp.Message == SAVE_POINTERS || current_command->SCp.Message == DISCONNECT)
  470. outb(SET_ATN, base + COMMAND_REG);
  471. outb(MSG_ACCEPTED, base + COMMAND_REG);
  472. break;
  473. }
  474. }
  475. out:
  476. return IRQ_HANDLED;
  477. }
  478. static void sym53c416_init(int base, int scsi_id)
  479. {
  480. outb(RESET_CHIP, base + COMMAND_REG);
  481. outb(NOOP, base + COMMAND_REG);
  482. outb(0x99, base + TOM); /* Time out of 250 ms */
  483. outb(0x05, base + STP);
  484. outb(0x00, base + SYNC_OFFSET);
  485. outb(EPC | scsi_id, base + CONF_REG_1);
  486. outb(FE | SCSI2 | TBPA, base + CONF_REG_2);
  487. outb(IDMRC | QTE | CDB10 | FSCSI | FCLK, base + CONF_REG_3);
  488. outb(0x83 | EAN, base + CONF_REG_4);
  489. outb(IE | WSE0, base + CONF_REG_5);
  490. outb(0, base + FEATURE_EN);
  491. }
  492. static int sym53c416_probeirq(int base, int scsi_id)
  493. {
  494. int irq, irqs;
  495. unsigned long i;
  496. /* Clear interrupt register */
  497. inb(base + INT_REG);
  498. /* Start probing for irq's */
  499. irqs = probe_irq_on();
  500. /* Reinit chip */
  501. sym53c416_init(base, scsi_id);
  502. /* Cause interrupt */
  503. outb(NOOP, base + COMMAND_REG);
  504. outb(ILLEGAL, base + COMMAND_REG);
  505. outb(0x07, base + DEST_BUS_ID);
  506. outb(0x00, base + DEST_BUS_ID);
  507. /* Wait for interrupt to occur */
  508. i = jiffies + 20;
  509. while(time_before(jiffies, i) && !(inb(base + STATUS_REG) & SCI))
  510. barrier();
  511. if(time_before_eq(i, jiffies)) /* timed out */
  512. return 0;
  513. /* Get occurred irq */
  514. irq = probe_irq_off(irqs);
  515. sym53c416_init(base, scsi_id);
  516. return irq;
  517. }
  518. /* Setup: sym53c416=base,irq */
  519. void sym53c416_setup(char *str, int *ints)
  520. {
  521. int i;
  522. if(host_index >= MAXHOSTS)
  523. {
  524. printk(KERN_WARNING "sym53c416: Too many hosts defined\n");
  525. return;
  526. }
  527. if(ints[0] < 1 || ints[0] > 2)
  528. {
  529. printk(KERN_ERR "sym53c416: Wrong number of parameters:\n");
  530. printk(KERN_ERR "sym53c416: usage: sym53c416=<base>[,<irq>]\n");
  531. return;
  532. }
  533. for(i = 0; i < host_index && i >= 0; i++)
  534. if(hosts[i].base == ints[1])
  535. i = -2;
  536. if(i >= 0)
  537. {
  538. hosts[host_index].base = ints[1];
  539. hosts[host_index].irq = (ints[0] == 2)? ints[2] : 0;
  540. host_index++;
  541. }
  542. }
  543. static int sym53c416_test(int base)
  544. {
  545. outb(RESET_CHIP, base + COMMAND_REG);
  546. outb(NOOP, base + COMMAND_REG);
  547. if(inb(base + COMMAND_REG) != NOOP)
  548. return 0;
  549. if(!inb(base + TC_HIGH) || inb(base + TC_HIGH) == 0xFF)
  550. return 0;
  551. if((inb(base + PIO_INT_REG) & (FULL | EMPTY | CE | OUE | FIE | EIE)) != EMPTY)
  552. return 0;
  553. return 1;
  554. }
  555. static struct isapnp_device_id id_table[] __devinitdata = {
  556. { ISAPNP_ANY_ID, ISAPNP_ANY_ID,
  557. ISAPNP_VENDOR('S','L','I'), ISAPNP_FUNCTION(0x4161), 0 },
  558. { ISAPNP_ANY_ID, ISAPNP_ANY_ID,
  559. ISAPNP_VENDOR('S','L','I'), ISAPNP_FUNCTION(0x4163), 0 },
  560. { ISAPNP_DEVICE_SINGLE_END }
  561. };
  562. MODULE_DEVICE_TABLE(isapnp, id_table);
  563. static void sym53c416_probe(void)
  564. {
  565. int *base = probeaddrs;
  566. int ints[2];
  567. ints[0] = 1;
  568. for(; *base; base++) {
  569. if (request_region(*base, IO_RANGE, ID)) {
  570. if (sym53c416_test(*base)) {
  571. ints[1] = *base;
  572. sym53c416_setup(NULL, ints);
  573. }
  574. release_region(*base, IO_RANGE);
  575. }
  576. }
  577. }
  578. int __init sym53c416_detect(struct scsi_host_template *tpnt)
  579. {
  580. unsigned long flags;
  581. struct Scsi_Host * shpnt = NULL;
  582. int i;
  583. int count;
  584. struct pnp_dev *idev = NULL;
  585. #ifdef MODULE
  586. int ints[3];
  587. ints[0] = 2;
  588. if(sym53c416_base)
  589. {
  590. ints[1] = sym53c416_base[0];
  591. ints[2] = sym53c416_base[1];
  592. sym53c416_setup(NULL, ints);
  593. }
  594. if(sym53c416_base_1)
  595. {
  596. ints[1] = sym53c416_base_1[0];
  597. ints[2] = sym53c416_base_1[1];
  598. sym53c416_setup(NULL, ints);
  599. }
  600. if(sym53c416_base_2)
  601. {
  602. ints[1] = sym53c416_base_2[0];
  603. ints[2] = sym53c416_base_2[1];
  604. sym53c416_setup(NULL, ints);
  605. }
  606. if(sym53c416_base_3)
  607. {
  608. ints[1] = sym53c416_base_3[0];
  609. ints[2] = sym53c416_base_3[1];
  610. sym53c416_setup(NULL, ints);
  611. }
  612. #endif
  613. printk(KERN_INFO "sym53c416.c: %s\n", VERSION_STRING);
  614. for (i=0; id_table[i].vendor != 0; i++) {
  615. while((idev=pnp_find_dev(NULL, id_table[i].vendor,
  616. id_table[i].function, idev))!=NULL)
  617. {
  618. int i[3];
  619. if(pnp_device_attach(idev)<0)
  620. {
  621. printk(KERN_WARNING "sym53c416: unable to attach PnP device.\n");
  622. continue;
  623. }
  624. if(pnp_activate_dev(idev) < 0)
  625. {
  626. printk(KERN_WARNING "sym53c416: unable to activate PnP device.\n");
  627. pnp_device_detach(idev);
  628. continue;
  629. }
  630. i[0] = 2;
  631. i[1] = pnp_port_start(idev, 0);
  632. i[2] = pnp_irq(idev, 0);
  633. printk(KERN_INFO "sym53c416: ISAPnP card found and configured at 0x%X, IRQ %d.\n",
  634. i[1], i[2]);
  635. sym53c416_setup(NULL, i);
  636. }
  637. }
  638. sym53c416_probe();
  639. /* Now we register and set up each host adapter found... */
  640. for(count = 0, i = 0; i < host_index; i++) {
  641. if (!request_region(hosts[i].base, IO_RANGE, ID))
  642. continue;
  643. if (!sym53c416_test(hosts[i].base)) {
  644. printk(KERN_WARNING "No sym53c416 found at address 0x%03x\n", hosts[i].base);
  645. goto fail_release_region;
  646. }
  647. /* We don't have an irq yet, so we should probe for one */
  648. if (!hosts[i].irq)
  649. hosts[i].irq = sym53c416_probeirq(hosts[i].base, hosts[i].scsi_id);
  650. if (!hosts[i].irq)
  651. goto fail_release_region;
  652. shpnt = scsi_register(tpnt, 0);
  653. if (!shpnt)
  654. goto fail_release_region;
  655. /* Request for specified IRQ */
  656. if (request_irq(hosts[i].irq, sym53c416_intr_handle, 0, ID, shpnt))
  657. goto fail_free_host;
  658. spin_lock_irqsave(&sym53c416_lock, flags);
  659. shpnt->unique_id = hosts[i].base;
  660. shpnt->io_port = hosts[i].base;
  661. shpnt->n_io_port = IO_RANGE;
  662. shpnt->irq = hosts[i].irq;
  663. shpnt->this_id = hosts[i].scsi_id;
  664. sym53c416_init(hosts[i].base, hosts[i].scsi_id);
  665. count++;
  666. spin_unlock_irqrestore(&sym53c416_lock, flags);
  667. continue;
  668. fail_free_host:
  669. scsi_unregister(shpnt);
  670. fail_release_region:
  671. release_region(hosts[i].base, IO_RANGE);
  672. }
  673. return count;
  674. }
  675. const char *sym53c416_info(struct Scsi_Host *SChost)
  676. {
  677. int i;
  678. int base = SChost->io_port;
  679. int irq = SChost->irq;
  680. int scsi_id = 0;
  681. int rev = inb(base + TC_HIGH);
  682. for(i = 0; i < host_index; i++)
  683. if(hosts[i].base == base)
  684. scsi_id = hosts[i].scsi_id;
  685. sprintf(info, "Symbios Logic 53c416 (rev. %d) at 0x%03x, irq %d, SCSI-ID %d, %s pio", rev, base, irq, scsi_id, (fastpio)? "fast" : "slow");
  686. return info;
  687. }
  688. int sym53c416_queuecommand(Scsi_Cmnd *SCpnt, void (*done)(Scsi_Cmnd *))
  689. {
  690. int base;
  691. unsigned long flags = 0;
  692. int i;
  693. /* Store base register as we can have more than one controller in the system */
  694. base = SCpnt->device->host->io_port;
  695. current_command = SCpnt; /* set current command */
  696. current_command->scsi_done = done; /* set ptr to done function */
  697. current_command->SCp.phase = command_ph; /* currect phase is the command phase */
  698. current_command->SCp.Status = 0;
  699. current_command->SCp.Message = 0;
  700. spin_lock_irqsave(&sym53c416_lock, flags);
  701. outb(scmd_id(SCpnt), base + DEST_BUS_ID); /* Set scsi id target */
  702. outb(FLUSH_FIFO, base + COMMAND_REG); /* Flush SCSI and PIO FIFO's */
  703. /* Write SCSI command into the SCSI fifo */
  704. for(i = 0; i < SCpnt->cmd_len; i++)
  705. outb(SCpnt->cmnd[i], base + SCSI_FIFO);
  706. /* Start selection sequence */
  707. outb(SEL_WITHOUT_ATN_SEQ, base + COMMAND_REG);
  708. /* Now an interrupt will be generated which we will catch in out interrupt routine */
  709. spin_unlock_irqrestore(&sym53c416_lock, flags);
  710. return 0;
  711. }
  712. static int sym53c416_host_reset(Scsi_Cmnd *SCpnt)
  713. {
  714. int base;
  715. int scsi_id = -1;
  716. int i;
  717. unsigned long flags;
  718. spin_lock_irqsave(&sym53c416_lock, flags);
  719. /* printk("sym53c416_reset\n"); */
  720. base = SCpnt->device->host->io_port;
  721. /* search scsi_id - fixme, we shouldnt need to iterate for this! */
  722. for(i = 0; i < host_index && scsi_id == -1; i++)
  723. if(hosts[i].base == base)
  724. scsi_id = hosts[i].scsi_id;
  725. outb(RESET_CHIP, base + COMMAND_REG);
  726. outb(NOOP | PIO_MODE, base + COMMAND_REG);
  727. outb(RESET_SCSI_BUS, base + COMMAND_REG);
  728. sym53c416_init(base, scsi_id);
  729. spin_unlock_irqrestore(&sym53c416_lock, flags);
  730. return SUCCESS;
  731. }
  732. static int sym53c416_release(struct Scsi_Host *shost)
  733. {
  734. if (shost->irq)
  735. free_irq(shost->irq, shost);
  736. if (shost->io_port && shost->n_io_port)
  737. release_region(shost->io_port, shost->n_io_port);
  738. return 0;
  739. }
  740. static int sym53c416_bios_param(struct scsi_device *sdev,
  741. struct block_device *dev,
  742. sector_t capacity, int *ip)
  743. {
  744. int size;
  745. size = capacity;
  746. ip[0] = 64; /* heads */
  747. ip[1] = 32; /* sectors */
  748. if((ip[2] = size >> 11) > 1024) /* cylinders, test for big disk */
  749. {
  750. ip[0] = 255; /* heads */
  751. ip[1] = 63; /* sectors */
  752. ip[2] = size / (255 * 63); /* cylinders */
  753. }
  754. return 0;
  755. }
  756. /* Loadable module support */
  757. #ifdef MODULE
  758. MODULE_AUTHOR("Lieven Willems");
  759. MODULE_LICENSE("GPL");
  760. module_param_array(sym53c416, uint, NULL, 0);
  761. module_param_array(sym53c416_1, uint, NULL, 0);
  762. module_param_array(sym53c416_2, uint, NULL, 0);
  763. module_param_array(sym53c416_3, uint, NULL, 0);
  764. #endif
  765. static struct scsi_host_template driver_template = {
  766. .proc_name = "sym53c416",
  767. .name = "Symbios Logic 53c416",
  768. .detect = sym53c416_detect,
  769. .info = sym53c416_info,
  770. .queuecommand = sym53c416_queuecommand,
  771. .eh_host_reset_handler =sym53c416_host_reset,
  772. .release = sym53c416_release,
  773. .bios_param = sym53c416_bios_param,
  774. .can_queue = 1,
  775. .this_id = SYM53C416_SCSI_ID,
  776. .sg_tablesize = 32,
  777. .cmd_per_lun = 1,
  778. .unchecked_isa_dma = 1,
  779. .use_clustering = ENABLE_CLUSTERING,
  780. };
  781. #include "scsi_module.c"