sym53c8xx.c 28 KB

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