sym53c8xx.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875
  1. /*
  2. * (C) Copyright 2001
  3. * Denis Peter, MPL AG Switzerland, d.peter@mpl.ch.
  4. *
  5. * See file CREDITS for list of people who contributed to this
  6. * project.
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License as
  10. * published by the Free Software Foundation; either version 2 of
  11. * the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  21. * MA 02111-1307 USA
  22. * partly derived from
  23. * linux/drivers/scsi/sym53c8xx.c
  24. *
  25. */
  26. /*
  27. * SCSI support based on the chip sym53C810.
  28. *
  29. * 09-19-2001 Andreas Heppel, Sysgo RTS GmbH <aheppel@sysgo.de>
  30. * The local version of this driver for the BAB750 board does not
  31. * use interrupts but polls the chip instead (see the call of
  32. * 'handle_scsi_int()' in 'scsi_issue()'.
  33. */
  34. #include <common.h>
  35. #ifdef CONFIG_SCSI_SYM53C8XX
  36. #include <command.h>
  37. #include <pci.h>
  38. #include <asm/processor.h>
  39. #include <sym53c8xx.h>
  40. #include <scsi.h>
  41. #undef SYM53C8XX_DEBUG
  42. #ifdef SYM53C8XX_DEBUG
  43. #define PRINTF(fmt,args...) printf (fmt ,##args)
  44. #else
  45. #define PRINTF(fmt,args...)
  46. #endif
  47. #if defined(CONFIG_CMD_SCSI) && defined(CONFIG_SCSI_SYM53C8XX)
  48. #undef SCSI_SINGLE_STEP
  49. /*
  50. * Single Step is only used for debug purposes
  51. */
  52. #ifdef SCSI_SINGLE_STEP
  53. static unsigned long start_script_select;
  54. static unsigned long start_script_msgout;
  55. static unsigned long start_script_msgin;
  56. static unsigned long start_script_msg_ext;
  57. static unsigned long start_script_cmd;
  58. static unsigned long start_script_data_in;
  59. static unsigned long start_script_data_out;
  60. static unsigned long start_script_status;
  61. static unsigned long start_script_complete;
  62. static unsigned long start_script_error;
  63. static unsigned long start_script_reselection;
  64. static unsigned int len_script_select;
  65. static unsigned int len_script_msgout;
  66. static unsigned int len_script_msgin;
  67. static unsigned int len_script_msg_ext;
  68. static unsigned int len_script_cmd;
  69. static unsigned int len_script_data_in;
  70. static unsigned int len_script_data_out;
  71. static unsigned int len_script_status;
  72. static unsigned int len_script_complete;
  73. static unsigned int len_script_error;
  74. static unsigned int len_script_reselection;
  75. #endif
  76. static unsigned short scsi_int_mask; /* shadow register for SCSI related interrupts */
  77. static unsigned char script_int_mask; /* shadow register for SCRIPT related interrupts */
  78. static unsigned long script_select[8]; /* script for selection */
  79. static unsigned long script_msgout[8]; /* script for message out phase (NOT USED) */
  80. static unsigned long script_msgin[14]; /* script for message in phase */
  81. static unsigned long script_msg_ext[32]; /* script for message in phase when more than 1 byte message */
  82. static unsigned long script_cmd[18]; /* script for command phase */
  83. static unsigned long script_data_in[8]; /* script for data in phase */
  84. static unsigned long script_data_out[8]; /* script for data out phase */
  85. static unsigned long script_status[6]; /* script for status phase */
  86. static unsigned long script_complete[10]; /* script for complete */
  87. static unsigned long script_reselection[4]; /* script for reselection (NOT USED) */
  88. static unsigned long script_error[2]; /* script for error handling */
  89. static unsigned long int_stat[3]; /* interrupt status */
  90. static unsigned long scsi_mem_addr; /* base memory address =SCSI_MEM_ADDRESS; */
  91. #define bus_to_phys(a) pci_mem_to_phys(busdevfunc, (unsigned long) (a))
  92. #define phys_to_bus(a) pci_phys_to_mem(busdevfunc, (unsigned long) (a))
  93. #define SCSI_MAX_RETRY 3 /* number of retries in scsi_issue() */
  94. #define SCSI_MAX_RETRY_NOT_READY 10 /* number of retries when device is not ready */
  95. #define SCSI_NOT_READY_TIME_OUT 500 /* timeout per retry when not ready */
  96. /*********************************************************************************
  97. * forward declerations
  98. */
  99. void scsi_chip_init(void);
  100. void handle_scsi_int(void);
  101. /********************************************************************************
  102. * reports SCSI errors to the user
  103. */
  104. void scsi_print_error (ccb * pccb)
  105. {
  106. int i;
  107. printf ("SCSI Error: Target %d LUN %d Command %02X\n", pccb->target,
  108. pccb->lun, pccb->cmd[0]);
  109. printf (" CCB: ");
  110. for (i = 0; i < pccb->cmdlen; i++)
  111. printf ("%02X ", pccb->cmd[i]);
  112. printf ("(len=%d)\n", pccb->cmdlen);
  113. printf (" Cntrl: ");
  114. switch (pccb->contr_stat) {
  115. case SIR_COMPLETE:
  116. printf ("Complete (no Error)\n");
  117. break;
  118. case SIR_SEL_ATN_NO_MSG_OUT:
  119. printf ("Selected with ATN no MSG out phase\n");
  120. break;
  121. case SIR_CMD_OUT_ILL_PH:
  122. printf ("Command out illegal phase\n");
  123. break;
  124. case SIR_MSG_RECEIVED:
  125. printf ("MSG received Error\n");
  126. break;
  127. case SIR_DATA_IN_ERR:
  128. printf ("Data in Error\n");
  129. break;
  130. case SIR_DATA_OUT_ERR:
  131. printf ("Data out Error\n");
  132. break;
  133. case SIR_SCRIPT_ERROR:
  134. printf ("Script Error\n");
  135. break;
  136. case SIR_MSG_OUT_NO_CMD:
  137. printf ("MSG out no Command phase\n");
  138. break;
  139. case SIR_MSG_OVER7:
  140. printf ("MSG in over 7 bytes\n");
  141. break;
  142. case INT_ON_FY:
  143. printf ("Interrupt on fly\n");
  144. break;
  145. case SCSI_SEL_TIME_OUT:
  146. printf ("SCSI Selection Timeout\n");
  147. break;
  148. case SCSI_HNS_TIME_OUT:
  149. printf ("SCSI Handshake Timeout\n");
  150. break;
  151. case SCSI_MA_TIME_OUT:
  152. printf ("SCSI Phase Error\n");
  153. break;
  154. case SCSI_UNEXP_DIS:
  155. printf ("SCSI unexpected disconnect\n");
  156. break;
  157. default:
  158. printf ("unknown status %lx\n", pccb->contr_stat);
  159. break;
  160. }
  161. printf (" Sense: SK %x (", pccb->sense_buf[2] & 0x0f);
  162. switch (pccb->sense_buf[2] & 0xf) {
  163. case SENSE_NO_SENSE:
  164. printf ("No Sense)");
  165. break;
  166. case SENSE_RECOVERED_ERROR:
  167. printf ("Recovered Error)");
  168. break;
  169. case SENSE_NOT_READY:
  170. printf ("Not Ready)");
  171. break;
  172. case SENSE_MEDIUM_ERROR:
  173. printf ("Medium Error)");
  174. break;
  175. case SENSE_HARDWARE_ERROR:
  176. printf ("Hardware Error)");
  177. break;
  178. case SENSE_ILLEGAL_REQUEST:
  179. printf ("Illegal request)");
  180. break;
  181. case SENSE_UNIT_ATTENTION:
  182. printf ("Unit Attention)");
  183. break;
  184. case SENSE_DATA_PROTECT:
  185. printf ("Data Protect)");
  186. break;
  187. case SENSE_BLANK_CHECK:
  188. printf ("Blank check)");
  189. break;
  190. case SENSE_VENDOR_SPECIFIC:
  191. printf ("Vendor specific)");
  192. break;
  193. case SENSE_COPY_ABORTED:
  194. printf ("Copy aborted)");
  195. break;
  196. case SENSE_ABORTED_COMMAND:
  197. printf ("Aborted Command)");
  198. break;
  199. case SENSE_VOLUME_OVERFLOW:
  200. printf ("Volume overflow)");
  201. break;
  202. case SENSE_MISCOMPARE:
  203. printf ("Misscompare\n");
  204. break;
  205. default:
  206. printf ("Illegal Sensecode\n");
  207. break;
  208. }
  209. printf (" ASC %x ASCQ %x\n", pccb->sense_buf[12],
  210. pccb->sense_buf[13]);
  211. printf (" Status: ");
  212. switch (pccb->status) {
  213. case S_GOOD:
  214. printf ("Good\n");
  215. break;
  216. case S_CHECK_COND:
  217. printf ("Check condition\n");
  218. break;
  219. case S_COND_MET:
  220. printf ("Condition Met\n");
  221. break;
  222. case S_BUSY:
  223. printf ("Busy\n");
  224. break;
  225. case S_INT:
  226. printf ("Intermediate\n");
  227. break;
  228. case S_INT_COND_MET:
  229. printf ("Intermediate condition met\n");
  230. break;
  231. case S_CONFLICT:
  232. printf ("Reservation conflict\n");
  233. break;
  234. case S_TERMINATED:
  235. printf ("Command terminated\n");
  236. break;
  237. case S_QUEUE_FULL:
  238. printf ("Task set full\n");
  239. break;
  240. default:
  241. printf ("unknown: %02X\n", pccb->status);
  242. break;
  243. }
  244. }
  245. /******************************************************************************
  246. * sets-up the SCSI controller
  247. * the base memory address is retrived via the pci_read_config_dword
  248. */
  249. void scsi_low_level_init(int busdevfunc)
  250. {
  251. unsigned int cmd;
  252. unsigned int addr;
  253. unsigned char vec;
  254. pci_read_config_byte(busdevfunc, PCI_INTERRUPT_LINE, &vec);
  255. pci_read_config_dword(busdevfunc, PCI_BASE_ADDRESS_1, &addr);
  256. addr = bus_to_phys(addr & ~0xf);
  257. /*
  258. * Enable bus mastering in case this has not been done, yet.
  259. */
  260. pci_read_config_dword(busdevfunc, PCI_COMMAND, &cmd);
  261. cmd |= PCI_COMMAND_MASTER;
  262. pci_write_config_dword(busdevfunc, PCI_COMMAND, cmd);
  263. scsi_mem_addr = addr;
  264. scsi_chip_init();
  265. scsi_bus_reset();
  266. }
  267. /************************************************************************************
  268. * Low level Part of SCSI Driver
  269. */
  270. /*
  271. * big-endian -> little endian conversion for the script
  272. */
  273. unsigned long swap_script(unsigned long val)
  274. {
  275. unsigned long tmp;
  276. tmp = ((val>>24)&0xff) | ((val>>8)&0xff00) | ((val<<8)&0xff0000) | ((val<<24)&0xff000000);
  277. return tmp;
  278. }
  279. void scsi_write_byte(ulong offset,unsigned char val)
  280. {
  281. out8(scsi_mem_addr+offset,val);
  282. }
  283. unsigned char scsi_read_byte(ulong offset)
  284. {
  285. return(in8(scsi_mem_addr+offset));
  286. }
  287. /********************************************************************************
  288. * interrupt handler
  289. */
  290. void handle_scsi_int(void)
  291. {
  292. unsigned char stat,stat1,stat2;
  293. unsigned short sstat;
  294. int i;
  295. #ifdef SCSI_SINGLE_STEP
  296. unsigned long tt;
  297. #endif
  298. stat=scsi_read_byte(ISTAT);
  299. if((stat & DIP)==DIP) { /* DMA Interrupt pending */
  300. stat1=scsi_read_byte(DSTAT);
  301. #ifdef SCSI_SINGLE_STEP
  302. if((stat1 & SSI)==SSI) {
  303. tt=in32r(scsi_mem_addr+DSP);
  304. if(((tt)>=start_script_select) && ((tt)<start_script_select+len_script_select)) {
  305. printf("select %d\n",(tt-start_script_select)>>2);
  306. goto end_single;
  307. }
  308. if(((tt)>=start_script_msgout) && ((tt)<start_script_msgout+len_script_msgout)) {
  309. printf("msgout %d\n",(tt-start_script_msgout)>>2);
  310. goto end_single;
  311. }
  312. if(((tt)>=start_script_msgin) && ((tt)<start_script_msgin+len_script_msgin)) {
  313. printf("msgin %d\n",(tt-start_script_msgin)>>2);
  314. goto end_single;
  315. }
  316. if(((tt)>=start_script_msg_ext) && ((tt)<start_script_msg_ext+len_script_msg_ext)) {
  317. printf("msgin_ext %d\n",(tt-start_script_msg_ext)>>2);
  318. goto end_single;
  319. }
  320. if(((tt)>=start_script_cmd) && ((tt)<start_script_cmd+len_script_cmd)) {
  321. printf("cmd %d\n",(tt-start_script_cmd)>>2);
  322. goto end_single;
  323. }
  324. if(((tt)>=start_script_data_in) && ((tt)<start_script_data_in+len_script_data_in)) {
  325. printf("data_in %d\n",(tt-start_script_data_in)>>2);
  326. goto end_single;
  327. }
  328. if(((tt)>=start_script_data_out) && ((tt)<start_script_data_out+len_script_data_out)) {
  329. printf("data_out %d\n",(tt-start_script_data_out)>>2);
  330. goto end_single;
  331. }
  332. if(((tt)>=start_script_status) && ((tt)<start_script_status+len_script_status)) {
  333. printf("status %d\n",(tt-start_script_status)>>2);
  334. goto end_single;
  335. }
  336. if(((tt)>=start_script_complete) && ((tt)<start_script_complete+len_script_complete)) {
  337. printf("complete %d\n",(tt-start_script_complete)>>2);
  338. goto end_single;
  339. }
  340. if(((tt)>=start_script_error) && ((tt)<start_script_error+len_script_error)) {
  341. printf("error %d\n",(tt-start_script_error)>>2);
  342. goto end_single;
  343. }
  344. if(((tt)>=start_script_reselection) && ((tt)<start_script_reselection+len_script_reselection)) {
  345. printf("reselection %d\n",(tt-start_script_reselection)>>2);
  346. goto end_single;
  347. }
  348. printf("sc: %lx\n",tt);
  349. end_single:
  350. stat2=scsi_read_byte(DCNTL);
  351. stat2|=STD;
  352. scsi_write_byte(DCNTL,stat2);
  353. }
  354. #endif
  355. if((stat1 & SIR)==SIR) /* script interrupt */
  356. {
  357. int_stat[0]=in32(scsi_mem_addr+DSPS);
  358. }
  359. if((stat1 & DFE)==0) { /* fifo not epmty */
  360. scsi_write_byte(CTEST3,CLF); /* Clear DMA FIFO */
  361. stat2=scsi_read_byte(STEST3);
  362. scsi_write_byte(STEST3,(stat2 | CSF)); /* Clear SCSI FIFO */
  363. }
  364. }
  365. if((stat & SIP)==SIP) { /* scsi interrupt */
  366. sstat = (unsigned short)scsi_read_byte(SIST+1);
  367. sstat <<=8;
  368. sstat |= (unsigned short)scsi_read_byte(SIST);
  369. for(i=0;i<3;i++) {
  370. if(int_stat[i]==0)
  371. break; /* found an empty int status */
  372. }
  373. int_stat[i]=SCSI_INT_STATE | sstat;
  374. stat1=scsi_read_byte(DSTAT);
  375. if((stat1 & DFE)==0) { /* fifo not epmty */
  376. scsi_write_byte(CTEST3,CLF); /* Clear DMA FIFO */
  377. stat2=scsi_read_byte(STEST3);
  378. scsi_write_byte(STEST3,(stat2 | CSF)); /* Clear SCSI FIFO */
  379. }
  380. }
  381. if((stat & INTF)==INTF) { /* interrupt on Fly */
  382. scsi_write_byte(ISTAT,stat); /* clear it */
  383. for(i=0;i<3;i++) {
  384. if(int_stat[i]==0)
  385. break; /* found an empty int status */
  386. }
  387. int_stat[i]=INT_ON_FY;
  388. }
  389. }
  390. void scsi_bus_reset(void)
  391. {
  392. unsigned char t;
  393. int i;
  394. int end = CFG_SCSI_SPIN_UP_TIME*1000;
  395. t=scsi_read_byte(SCNTL1);
  396. scsi_write_byte(SCNTL1,(t | CRST));
  397. udelay(50);
  398. scsi_write_byte(SCNTL1,t);
  399. puts("waiting for devices to spin up");
  400. for(i=0;i<end;i++) {
  401. udelay(1000); /* give the devices time to spin up */
  402. if (i % 1000 == 0)
  403. putc('.');
  404. }
  405. putc('\n');
  406. scsi_chip_init(); /* reinit the chip ...*/
  407. }
  408. void scsi_int_enable(void)
  409. {
  410. scsi_write_byte(SIEN,(unsigned char)scsi_int_mask);
  411. scsi_write_byte(SIEN+1,(unsigned char)(scsi_int_mask>>8));
  412. scsi_write_byte(DIEN,script_int_mask);
  413. }
  414. void scsi_write_dsp(unsigned long start)
  415. {
  416. unsigned long val;
  417. #ifdef SCSI_SINGLE_STEP
  418. unsigned char t;
  419. #endif
  420. val = start;
  421. out32r(scsi_mem_addr + DSP,start);
  422. #ifdef SCSI_SINGLE_STEP
  423. t=scsi_read_byte(DCNTL);
  424. t|=STD;
  425. scsi_write_byte(DCNTL,t);
  426. #endif
  427. }
  428. /* only used for debug purposes */
  429. void scsi_print_script(void)
  430. {
  431. printf("script_select @ 0x%08lX\n",(unsigned long)&script_select[0]);
  432. printf("script_msgout @ 0x%08lX\n",(unsigned long)&script_msgout[0]);
  433. printf("script_msgin @ 0x%08lX\n",(unsigned long)&script_msgin[0]);
  434. printf("script_msgext @ 0x%08lX\n",(unsigned long)&script_msg_ext[0]);
  435. printf("script_cmd @ 0x%08lX\n",(unsigned long)&script_cmd[0]);
  436. printf("script_data_in @ 0x%08lX\n",(unsigned long)&script_data_in[0]);
  437. printf("script_data_out @ 0x%08lX\n",(unsigned long)&script_data_out[0]);
  438. printf("script_status @ 0x%08lX\n",(unsigned long)&script_status[0]);
  439. printf("script_complete @ 0x%08lX\n",(unsigned long)&script_complete[0]);
  440. printf("script_error @ 0x%08lX\n",(unsigned long)&script_error[0]);
  441. }
  442. void scsi_set_script(ccb *pccb)
  443. {
  444. int busdevfunc = pccb->priv;
  445. int i;
  446. i=0;
  447. script_select[i++]=swap_script(SCR_REG_REG(GPREG, SCR_AND, 0xfe));
  448. script_select[i++]=0; /* LED ON */
  449. script_select[i++]=swap_script(SCR_CLR(SCR_TRG)); /* select initiator mode */
  450. script_select[i++]=0;
  451. /* script_select[i++]=swap_script(SCR_SEL_ABS_ATN | pccb->target << 16); */
  452. script_select[i++]=swap_script(SCR_SEL_ABS | pccb->target << 16);
  453. script_select[i++]=swap_script(phys_to_bus(&script_cmd[4])); /* error handling */
  454. script_select[i++]=swap_script(SCR_JUMP); /* next section */
  455. /* script_select[i++]=swap_script((unsigned long)&script_msgout[0]); */ /* message out */
  456. script_select[i++]=swap_script(phys_to_bus(&script_cmd[0])); /* command out */
  457. #ifdef SCSI_SINGLE_STEP
  458. start_script_select=(unsigned long)&script_select[0];
  459. len_script_select=i*4;
  460. #endif
  461. i=0;
  462. script_msgout[i++]=swap_script(SCR_INT ^ IFFALSE (WHEN (SCR_MSG_OUT)));
  463. script_msgout[i++]=SIR_SEL_ATN_NO_MSG_OUT;
  464. script_msgout[i++]=swap_script( SCR_MOVE_ABS(1) ^ SCR_MSG_OUT);
  465. script_msgout[i++]=swap_script(phys_to_bus(&pccb->msgout[0]));
  466. script_msgout[i++]=swap_script(SCR_JUMP ^ IFTRUE (WHEN (SCR_COMMAND))); /* if Command phase */
  467. script_msgout[i++]=swap_script(phys_to_bus(&script_cmd[0])); /* switch to command */
  468. script_msgout[i++]=swap_script(SCR_INT); /* interrupt if not */
  469. script_msgout[i++]=SIR_MSG_OUT_NO_CMD;
  470. #ifdef SCSI_SINGLE_STEP
  471. start_script_msgout=(unsigned long)&script_msgout[0];
  472. len_script_msgout=i*4;
  473. #endif
  474. i=0;
  475. script_cmd[i++]=swap_script(SCR_MOVE_ABS(pccb->cmdlen) ^ SCR_COMMAND);
  476. script_cmd[i++]=swap_script(phys_to_bus(&pccb->cmd[0]));
  477. script_cmd[i++]=swap_script(SCR_JUMP ^ IFTRUE (WHEN (SCR_MSG_IN))); /* message in ? */
  478. script_cmd[i++]=swap_script(phys_to_bus(&script_msgin[0]));
  479. script_cmd[i++]=swap_script(SCR_JUMP ^ IFTRUE (IF (SCR_DATA_OUT))); /* data out ? */
  480. script_cmd[i++]=swap_script(phys_to_bus(&script_data_out[0]));
  481. script_cmd[i++]=swap_script(SCR_JUMP ^ IFTRUE (IF (SCR_DATA_IN))); /* data in ? */
  482. script_cmd[i++]=swap_script(phys_to_bus(&script_data_in[0]));
  483. script_cmd[i++]=swap_script(SCR_JUMP ^ IFTRUE (IF (SCR_STATUS))); /* status ? */
  484. script_cmd[i++]=swap_script(phys_to_bus(&script_status[0]));
  485. script_cmd[i++]=swap_script(SCR_JUMP ^ IFTRUE (IF (SCR_COMMAND))); /* command ? */
  486. script_cmd[i++]=swap_script(phys_to_bus(&script_cmd[0]));
  487. script_cmd[i++]=swap_script(SCR_JUMP ^ IFTRUE (IF (SCR_MSG_OUT))); /* message out ? */
  488. script_cmd[i++]=swap_script(phys_to_bus(&script_msgout[0]));
  489. script_cmd[i++]=swap_script(SCR_JUMP ^ IFTRUE (IF (SCR_MSG_IN))); /* just for error handling message in ? */
  490. script_cmd[i++]=swap_script(phys_to_bus(&script_msgin[0]));
  491. script_cmd[i++]=swap_script(SCR_INT); /* interrupt if not */
  492. script_cmd[i++]=SIR_CMD_OUT_ILL_PH;
  493. #ifdef SCSI_SINGLE_STEP
  494. start_script_cmd=(unsigned long)&script_cmd[0];
  495. len_script_cmd=i*4;
  496. #endif
  497. i=0;
  498. script_data_out[i++]=swap_script(SCR_MOVE_ABS(pccb->datalen)^ SCR_DATA_OUT); /* move */
  499. script_data_out[i++]=swap_script(phys_to_bus(pccb->pdata)); /* pointer to buffer */
  500. script_data_out[i++]=swap_script(SCR_JUMP ^ IFTRUE (WHEN (SCR_STATUS)));
  501. script_data_out[i++]=swap_script(phys_to_bus(&script_status[0]));
  502. script_data_out[i++]=swap_script(SCR_INT);
  503. script_data_out[i++]=SIR_DATA_OUT_ERR;
  504. #ifdef SCSI_SINGLE_STEP
  505. start_script_data_out=(unsigned long)&script_data_out[0];
  506. len_script_data_out=i*4;
  507. #endif
  508. i=0;
  509. script_data_in[i++]=swap_script(SCR_MOVE_ABS(pccb->datalen)^ SCR_DATA_IN); /* move */
  510. script_data_in[i++]=swap_script(phys_to_bus(pccb->pdata)); /* pointer to buffer */
  511. script_data_in[i++]=swap_script(SCR_JUMP ^ IFTRUE (WHEN (SCR_STATUS)));
  512. script_data_in[i++]=swap_script(phys_to_bus(&script_status[0]));
  513. script_data_in[i++]=swap_script(SCR_INT);
  514. script_data_in[i++]=SIR_DATA_IN_ERR;
  515. #ifdef SCSI_SINGLE_STEP
  516. start_script_data_in=(unsigned long)&script_data_in[0];
  517. len_script_data_in=i*4;
  518. #endif
  519. i=0;
  520. script_msgin[i++]=swap_script(SCR_MOVE_ABS (1) ^ SCR_MSG_IN);
  521. script_msgin[i++]=swap_script(phys_to_bus(&pccb->msgin[0]));
  522. script_msgin[i++]=swap_script(SCR_JUMP ^ IFTRUE (DATA (M_COMPLETE)));
  523. script_msgin[i++]=swap_script(phys_to_bus(&script_complete[0]));
  524. script_msgin[i++]=swap_script(SCR_JUMP ^ IFTRUE (DATA (M_DISCONNECT)));
  525. script_msgin[i++]=swap_script(phys_to_bus(&script_complete[0]));
  526. script_msgin[i++]=swap_script(SCR_JUMP ^ IFTRUE (DATA (M_SAVE_DP)));
  527. script_msgin[i++]=swap_script(phys_to_bus(&script_complete[0]));
  528. script_msgin[i++]=swap_script(SCR_JUMP ^ IFTRUE (DATA (M_RESTORE_DP)));
  529. script_msgin[i++]=swap_script(phys_to_bus(&script_complete[0]));
  530. script_msgin[i++]=swap_script(SCR_JUMP ^ IFTRUE (DATA (M_EXTENDED)));
  531. script_msgin[i++]=swap_script(phys_to_bus(&script_msg_ext[0]));
  532. script_msgin[i++]=swap_script(SCR_INT);
  533. script_msgin[i++]=SIR_MSG_RECEIVED;
  534. #ifdef SCSI_SINGLE_STEP
  535. start_script_msgin=(unsigned long)&script_msgin[0];
  536. len_script_msgin=i*4;
  537. #endif
  538. i=0;
  539. script_msg_ext[i++]=swap_script(SCR_CLR (SCR_ACK)); /* clear ACK */
  540. script_msg_ext[i++]=0;
  541. script_msg_ext[i++]=swap_script(SCR_MOVE_ABS (1) ^ SCR_MSG_IN); /* assuming this is the msg length */
  542. script_msg_ext[i++]=swap_script(phys_to_bus(&pccb->msgin[1]));
  543. script_msg_ext[i++]=swap_script(SCR_JUMP ^ IFFALSE (IF (SCR_MSG_IN)));
  544. script_msg_ext[i++]=swap_script(phys_to_bus(&script_complete[0])); /* no more bytes */
  545. script_msg_ext[i++]=swap_script(SCR_MOVE_ABS (1) ^ SCR_MSG_IN); /* next */
  546. script_msg_ext[i++]=swap_script(phys_to_bus(&pccb->msgin[2]));
  547. script_msg_ext[i++]=swap_script(SCR_JUMP ^ IFFALSE (IF (SCR_MSG_IN)));
  548. script_msg_ext[i++]=swap_script(phys_to_bus(&script_complete[0])); /* no more bytes */
  549. script_msg_ext[i++]=swap_script(SCR_MOVE_ABS (1) ^ SCR_MSG_IN); /* next */
  550. script_msg_ext[i++]=swap_script(phys_to_bus(&pccb->msgin[3]));
  551. script_msg_ext[i++]=swap_script(SCR_JUMP ^ IFFALSE (IF (SCR_MSG_IN)));
  552. script_msg_ext[i++]=swap_script(phys_to_bus(&script_complete[0])); /* no more bytes */
  553. script_msg_ext[i++]=swap_script(SCR_MOVE_ABS (1) ^ SCR_MSG_IN); /* next */
  554. script_msg_ext[i++]=swap_script(phys_to_bus(&pccb->msgin[4]));
  555. script_msg_ext[i++]=swap_script(SCR_JUMP ^ IFFALSE (IF (SCR_MSG_IN)));
  556. script_msg_ext[i++]=swap_script(phys_to_bus(&script_complete[0])); /* no more bytes */
  557. script_msg_ext[i++]=swap_script(SCR_MOVE_ABS (1) ^ SCR_MSG_IN); /* next */
  558. script_msg_ext[i++]=swap_script(phys_to_bus(&pccb->msgin[5]));
  559. script_msg_ext[i++]=swap_script(SCR_JUMP ^ IFFALSE (IF (SCR_MSG_IN)));
  560. script_msg_ext[i++]=swap_script(phys_to_bus(&script_complete[0])); /* no more bytes */
  561. script_msg_ext[i++]=swap_script(SCR_MOVE_ABS (1) ^ SCR_MSG_IN); /* next */
  562. script_msg_ext[i++]=swap_script(phys_to_bus(&pccb->msgin[6]));
  563. script_msg_ext[i++]=swap_script(SCR_JUMP ^ IFFALSE (IF (SCR_MSG_IN)));
  564. script_msg_ext[i++]=swap_script(phys_to_bus(&script_complete[0])); /* no more bytes */
  565. script_msg_ext[i++]=swap_script(SCR_MOVE_ABS (1) ^ SCR_MSG_IN); /* next */
  566. script_msg_ext[i++]=swap_script(phys_to_bus(&pccb->msgin[7]));
  567. script_msg_ext[i++]=swap_script(SCR_JUMP ^ IFFALSE (IF (SCR_MSG_IN)));
  568. script_msg_ext[i++]=swap_script(phys_to_bus(&script_complete[0])); /* no more bytes */
  569. script_msg_ext[i++]=swap_script(SCR_INT);
  570. script_msg_ext[i++]=SIR_MSG_OVER7;
  571. #ifdef SCSI_SINGLE_STEP
  572. start_script_msg_ext=(unsigned long)&script_msg_ext[0];
  573. len_script_msg_ext=i*4;
  574. #endif
  575. i=0;
  576. script_status[i++]=swap_script(SCR_MOVE_ABS (1) ^ SCR_STATUS);
  577. script_status[i++]=swap_script(phys_to_bus(&pccb->status));
  578. script_status[i++]=swap_script(SCR_JUMP ^ IFTRUE (WHEN (SCR_MSG_IN)));
  579. script_status[i++]=swap_script(phys_to_bus(&script_msgin[0]));
  580. script_status[i++]=swap_script(SCR_INT);
  581. script_status[i++]=SIR_STATUS_ILL_PH;
  582. #ifdef SCSI_SINGLE_STEP
  583. start_script_status=(unsigned long)&script_status[0];
  584. len_script_status=i*4;
  585. #endif
  586. i=0;
  587. script_complete[i++]=swap_script(SCR_REG_REG (SCNTL2, SCR_AND, 0x7f));
  588. script_complete[i++]=0;
  589. script_complete[i++]=swap_script(SCR_CLR (SCR_ACK|SCR_ATN));
  590. script_complete[i++]=0;
  591. script_complete[i++]=swap_script(SCR_WAIT_DISC);
  592. script_complete[i++]=0;
  593. script_complete[i++]=swap_script(SCR_REG_REG(GPREG, SCR_OR, 0x01));
  594. script_complete[i++]=0; /* LED OFF */
  595. script_complete[i++]=swap_script(SCR_INT);
  596. script_complete[i++]=SIR_COMPLETE;
  597. #ifdef SCSI_SINGLE_STEP
  598. start_script_complete=(unsigned long)&script_complete[0];
  599. len_script_complete=i*4;
  600. #endif
  601. i=0;
  602. script_error[i++]=swap_script(SCR_INT); /* interrupt if error */
  603. script_error[i++]=SIR_SCRIPT_ERROR;
  604. #ifdef SCSI_SINGLE_STEP
  605. start_script_error=(unsigned long)&script_error[0];
  606. len_script_error=i*4;
  607. #endif
  608. i=0;
  609. script_reselection[i++]=swap_script(SCR_CLR (SCR_TRG)); /* target status */
  610. script_reselection[i++]=0;
  611. script_reselection[i++]=swap_script(SCR_WAIT_RESEL);
  612. script_reselection[i++]=swap_script(phys_to_bus(&script_select[0])); /* len = 4 */
  613. #ifdef SCSI_SINGLE_STEP
  614. start_script_reselection=(unsigned long)&script_reselection[0];
  615. len_script_reselection=i*4;
  616. #endif
  617. }
  618. void scsi_issue(ccb *pccb)
  619. {
  620. int busdevfunc = pccb->priv;
  621. int i;
  622. unsigned short sstat;
  623. int retrycnt; /* retry counter */
  624. for(i=0;i<3;i++)
  625. int_stat[i]=0; /* delete all int status */
  626. /* struct pccb must be set-up correctly */
  627. retrycnt=0;
  628. PRINTF("ID %d issue cmd %02X\n",pccb->target,pccb->cmd[0]);
  629. pccb->trans_bytes=0; /* no bytes transfered yet */
  630. scsi_set_script(pccb); /* fill in SCRIPT */
  631. scsi_int_mask=STO | UDC | MA; /* | CMP; / * Interrupts which are enabled */
  632. script_int_mask=0xff; /* enable all Ints */
  633. scsi_int_enable();
  634. scsi_write_dsp(phys_to_bus(&script_select[0])); /* start script */
  635. /* now we have to wait for IRQs */
  636. retry:
  637. /*
  638. * This version of the driver is _not_ interrupt driven,
  639. * but polls the chip's interrupt registers (ISTAT, DSTAT).
  640. */
  641. while(int_stat[0]==0)
  642. handle_scsi_int();
  643. if(int_stat[0]==SIR_COMPLETE) {
  644. if(pccb->msgin[0]==M_DISCONNECT) {
  645. PRINTF("Wait for reselection\n");
  646. for(i=0;i<3;i++)
  647. int_stat[i]=0; /* delete all int status */
  648. scsi_write_dsp(phys_to_bus(&script_reselection[0])); /* start reselection script */
  649. goto retry;
  650. }
  651. pccb->contr_stat=SIR_COMPLETE;
  652. return;
  653. }
  654. if((int_stat[0] & SCSI_INT_STATE)==SCSI_INT_STATE) { /* scsi interrupt */
  655. sstat=(unsigned short)int_stat[0];
  656. if((sstat & STO)==STO) { /* selection timeout */
  657. pccb->contr_stat=SCSI_SEL_TIME_OUT;
  658. scsi_write_byte(GPREG,0x01);
  659. PRINTF("ID: %X Selection Timeout\n",pccb->target);
  660. return;
  661. }
  662. if((sstat & UDC)==UDC) { /* unexpected disconnect */
  663. pccb->contr_stat=SCSI_UNEXP_DIS;
  664. scsi_write_byte(GPREG,0x01);
  665. PRINTF("ID: %X Unexpected Disconnect\n",pccb->target);
  666. return;
  667. }
  668. if((sstat & RSL)==RSL) { /* reselection */
  669. pccb->contr_stat=SCSI_UNEXP_DIS;
  670. scsi_write_byte(GPREG,0x01);
  671. PRINTF("ID: %X Unexpected Disconnect\n",pccb->target);
  672. return;
  673. }
  674. if(((sstat & MA)==MA)||((sstat & HTH)==HTH)) { /* phase missmatch */
  675. if(retrycnt<SCSI_MAX_RETRY) {
  676. pccb->trans_bytes=pccb->datalen -
  677. ((unsigned long)scsi_read_byte(DBC) |
  678. ((unsigned long)scsi_read_byte(DBC+1)<<8) |
  679. ((unsigned long)scsi_read_byte(DBC+2)<<16));
  680. for(i=0;i<3;i++)
  681. int_stat[i]=0; /* delete all int status */
  682. retrycnt++;
  683. PRINTF("ID: %X Phase Missmatch Retry %d Phase %02X transfered %lx\n",
  684. pccb->target,retrycnt,scsi_read_byte(SBCL),pccb->trans_bytes);
  685. scsi_write_dsp(phys_to_bus(&script_cmd[4])); /* start retry script */
  686. goto retry;
  687. }
  688. if((sstat & MA)==MA)
  689. pccb->contr_stat=SCSI_MA_TIME_OUT;
  690. else
  691. pccb->contr_stat=SCSI_HNS_TIME_OUT;
  692. PRINTF("Phase Missmatch stat %lx\n",pccb->contr_stat);
  693. return;
  694. } /* no phase int */
  695. /* if((sstat & CMP)==CMP) {
  696. pccb->contr_stat=SIR_COMPLETE;
  697. return;
  698. }
  699. */
  700. PRINTF("SCSI INT %lX\n",int_stat[0]);
  701. pccb->contr_stat=int_stat[0];
  702. return;
  703. } /* end scsi int */
  704. PRINTF("SCRIPT INT %lX phase %02X\n",int_stat[0],scsi_read_byte(SBCL));
  705. pccb->contr_stat=int_stat[0];
  706. return;
  707. }
  708. int scsi_exec(ccb *pccb)
  709. {
  710. unsigned char tmpcmd[16],tmpstat;
  711. int i,retrycnt,t;
  712. unsigned long transbytes,datalen;
  713. unsigned char *tmpptr;
  714. retrycnt=0;
  715. retry:
  716. scsi_issue(pccb);
  717. if(pccb->contr_stat!=SIR_COMPLETE)
  718. return FALSE;
  719. if(pccb->status==S_GOOD)
  720. return TRUE;
  721. if(pccb->status==S_CHECK_COND) { /* check condition */
  722. for(i=0;i<16;i++)
  723. tmpcmd[i]=pccb->cmd[i];
  724. pccb->cmd[0]=SCSI_REQ_SENSE;
  725. pccb->cmd[1]=pccb->lun<<5;
  726. pccb->cmd[2]=0;
  727. pccb->cmd[3]=0;
  728. pccb->cmd[4]=14;
  729. pccb->cmd[5]=0;
  730. pccb->cmdlen=6;
  731. pccb->msgout[0]=SCSI_IDENTIFY;
  732. transbytes=pccb->trans_bytes;
  733. tmpptr=pccb->pdata;
  734. pccb->pdata = &pccb->sense_buf[0];
  735. datalen=pccb->datalen;
  736. pccb->datalen=14;
  737. tmpstat=pccb->status;
  738. scsi_issue(pccb);
  739. for(i=0;i<16;i++)
  740. pccb->cmd[i]=tmpcmd[i];
  741. pccb->trans_bytes=transbytes;
  742. pccb->pdata=tmpptr;
  743. pccb->datalen=datalen;
  744. pccb->status=tmpstat;
  745. PRINTF("Request_sense sense key %x ASC %x ASCQ %x\n",pccb->sense_buf[2]&0x0f,
  746. pccb->sense_buf[12],pccb->sense_buf[13]);
  747. switch(pccb->sense_buf[2]&0xf) {
  748. case SENSE_NO_SENSE:
  749. case SENSE_RECOVERED_ERROR:
  750. /* seems to be ok */
  751. return TRUE;
  752. break;
  753. case SENSE_NOT_READY:
  754. if((pccb->sense_buf[12]!=0x04)||(pccb->sense_buf[13]!=0x01)) {
  755. /* if device is not in process of becoming ready */
  756. return FALSE;
  757. break;
  758. } /* else fall through */
  759. case SENSE_UNIT_ATTENTION:
  760. if(retrycnt<SCSI_MAX_RETRY_NOT_READY) {
  761. PRINTF("Target %d not ready, retry %d\n",pccb->target,retrycnt);
  762. for(t=0;t<SCSI_NOT_READY_TIME_OUT;t++)
  763. udelay(1000); /* 1sec wait */
  764. retrycnt++;
  765. goto retry;
  766. }
  767. PRINTF("Target %d not ready, %d retried\n",pccb->target,retrycnt);
  768. return FALSE;
  769. default:
  770. return FALSE;
  771. }
  772. }
  773. PRINTF("Status = %X\n",pccb->status);
  774. return FALSE;
  775. }
  776. void scsi_chip_init(void)
  777. {
  778. /* first we issue a soft reset */
  779. scsi_write_byte(ISTAT,SRST);
  780. udelay(1000);
  781. scsi_write_byte(ISTAT,0);
  782. /* setup chip */
  783. scsi_write_byte(SCNTL0,0xC0); /* full arbitration no start, no message, parity disabled, master */
  784. scsi_write_byte(SCNTL1,0x00);
  785. scsi_write_byte(SCNTL2,0x00);
  786. #ifndef CFG_SCSI_SYM53C8XX_CCF /* config value for none 40 mhz clocks */
  787. scsi_write_byte(SCNTL3,0x13); /* synchronous clock 40/4=10MHz, asynchronous 40MHz */
  788. #else
  789. scsi_write_byte(SCNTL3,CFG_SCSI_SYM53C8XX_CCF); /* config value for none 40 mhz clocks */
  790. #endif
  791. scsi_write_byte(SCID,0x47); /* ID=7, enable reselection */
  792. scsi_write_byte(SXFER,0x00); /* synchronous transfer period 10MHz, asynchronous */
  793. scsi_write_byte(SDID,0x00); /* targed SCSI ID = 0 */
  794. scsi_int_mask=0x0000; /* no Interrupt is enabled */
  795. script_int_mask=0x00;
  796. scsi_int_enable();
  797. scsi_write_byte(GPREG,0x01); /* GPIO0 is LED (off) */
  798. scsi_write_byte(GPCNTL,0x0E); /* GPIO0 is Output */
  799. scsi_write_byte(STIME0,0x08); /* handshake timer disabled, selection timeout 512msec */
  800. scsi_write_byte(RESPID,0x80); /* repond only to the own ID (reselection) */
  801. scsi_write_byte(STEST1,0x00); /* not isolated, SCLK is used */
  802. scsi_write_byte(STEST2,0x00); /* no Lowlevel Mode? */
  803. scsi_write_byte(STEST3,0x80); /* enable tolerANT */
  804. scsi_write_byte(CTEST3,0x04); /* clear FIFO */
  805. scsi_write_byte(CTEST4,0x00);
  806. scsi_write_byte(CTEST5,0x00);
  807. #ifdef SCSI_SINGLE_STEP
  808. /* scsi_write_byte(DCNTL,IRQM | SSM); */
  809. scsi_write_byte(DCNTL,IRQD | SSM);
  810. scsi_write_byte(DMODE,MAN);
  811. #else
  812. /* scsi_write_byte(DCNTL,IRQM); */
  813. scsi_write_byte(DCNTL,IRQD);
  814. scsi_write_byte(DMODE,0x00);
  815. #endif
  816. }
  817. #endif
  818. #endif /* CONFIG_SCSI_SYM53C8XX */