sym53c8xx.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851
  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_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. return ((val >> 24) & 0xff) | ((val >> 8) & 0xff00) |
  259. ((val << 8) & 0xff0000) | ((val << 24) & 0xff000000);
  260. }
  261. void scsi_write_byte(ulong offset,unsigned char val)
  262. {
  263. out8(scsi_mem_addr+offset,val);
  264. }
  265. unsigned char scsi_read_byte(ulong offset)
  266. {
  267. return(in8(scsi_mem_addr+offset));
  268. }
  269. /********************************************************************************
  270. * interrupt handler
  271. */
  272. void handle_scsi_int(void)
  273. {
  274. unsigned char stat,stat1,stat2;
  275. unsigned short sstat;
  276. int i;
  277. #ifdef SCSI_SINGLE_STEP
  278. unsigned long tt;
  279. #endif
  280. stat=scsi_read_byte(ISTAT);
  281. if((stat & DIP)==DIP) { /* DMA Interrupt pending */
  282. stat1=scsi_read_byte(DSTAT);
  283. #ifdef SCSI_SINGLE_STEP
  284. if((stat1 & SSI)==SSI) {
  285. tt=in32r(scsi_mem_addr+DSP);
  286. if(((tt)>=start_script_select) && ((tt)<start_script_select+len_script_select)) {
  287. printf("select %d\n",(tt-start_script_select)>>2);
  288. goto end_single;
  289. }
  290. if(((tt)>=start_script_msgout) && ((tt)<start_script_msgout+len_script_msgout)) {
  291. printf("msgout %d\n",(tt-start_script_msgout)>>2);
  292. goto end_single;
  293. }
  294. if(((tt)>=start_script_msgin) && ((tt)<start_script_msgin+len_script_msgin)) {
  295. printf("msgin %d\n",(tt-start_script_msgin)>>2);
  296. goto end_single;
  297. }
  298. if(((tt)>=start_script_msg_ext) && ((tt)<start_script_msg_ext+len_script_msg_ext)) {
  299. printf("msgin_ext %d\n",(tt-start_script_msg_ext)>>2);
  300. goto end_single;
  301. }
  302. if(((tt)>=start_script_cmd) && ((tt)<start_script_cmd+len_script_cmd)) {
  303. printf("cmd %d\n",(tt-start_script_cmd)>>2);
  304. goto end_single;
  305. }
  306. if(((tt)>=start_script_data_in) && ((tt)<start_script_data_in+len_script_data_in)) {
  307. printf("data_in %d\n",(tt-start_script_data_in)>>2);
  308. goto end_single;
  309. }
  310. if(((tt)>=start_script_data_out) && ((tt)<start_script_data_out+len_script_data_out)) {
  311. printf("data_out %d\n",(tt-start_script_data_out)>>2);
  312. goto end_single;
  313. }
  314. if(((tt)>=start_script_status) && ((tt)<start_script_status+len_script_status)) {
  315. printf("status %d\n",(tt-start_script_status)>>2);
  316. goto end_single;
  317. }
  318. if(((tt)>=start_script_complete) && ((tt)<start_script_complete+len_script_complete)) {
  319. printf("complete %d\n",(tt-start_script_complete)>>2);
  320. goto end_single;
  321. }
  322. if(((tt)>=start_script_error) && ((tt)<start_script_error+len_script_error)) {
  323. printf("error %d\n",(tt-start_script_error)>>2);
  324. goto end_single;
  325. }
  326. if(((tt)>=start_script_reselection) && ((tt)<start_script_reselection+len_script_reselection)) {
  327. printf("reselection %d\n",(tt-start_script_reselection)>>2);
  328. goto end_single;
  329. }
  330. printf("sc: %lx\n",tt);
  331. end_single:
  332. stat2=scsi_read_byte(DCNTL);
  333. stat2|=STD;
  334. scsi_write_byte(DCNTL,stat2);
  335. }
  336. #endif
  337. if((stat1 & SIR)==SIR) /* script interrupt */
  338. {
  339. int_stat[0]=in32(scsi_mem_addr+DSPS);
  340. }
  341. if((stat1 & DFE)==0) { /* fifo not epmty */
  342. scsi_write_byte(CTEST3,CLF); /* Clear DMA FIFO */
  343. stat2=scsi_read_byte(STEST3);
  344. scsi_write_byte(STEST3,(stat2 | CSF)); /* Clear SCSI FIFO */
  345. }
  346. }
  347. if((stat & SIP)==SIP) { /* scsi interrupt */
  348. sstat = (unsigned short)scsi_read_byte(SIST+1);
  349. sstat <<=8;
  350. sstat |= (unsigned short)scsi_read_byte(SIST);
  351. for(i=0;i<3;i++) {
  352. if(int_stat[i]==0)
  353. break; /* found an empty int status */
  354. }
  355. int_stat[i]=SCSI_INT_STATE | sstat;
  356. stat1=scsi_read_byte(DSTAT);
  357. if((stat1 & DFE)==0) { /* fifo not epmty */
  358. scsi_write_byte(CTEST3,CLF); /* Clear DMA FIFO */
  359. stat2=scsi_read_byte(STEST3);
  360. scsi_write_byte(STEST3,(stat2 | CSF)); /* Clear SCSI FIFO */
  361. }
  362. }
  363. if((stat & INTF)==INTF) { /* interrupt on Fly */
  364. scsi_write_byte(ISTAT,stat); /* clear it */
  365. for(i=0;i<3;i++) {
  366. if(int_stat[i]==0)
  367. break; /* found an empty int status */
  368. }
  369. int_stat[i]=INT_ON_FY;
  370. }
  371. }
  372. void scsi_bus_reset(void)
  373. {
  374. unsigned char t;
  375. int i;
  376. int end = CONFIG_SYS_SCSI_SPIN_UP_TIME*1000;
  377. t=scsi_read_byte(SCNTL1);
  378. scsi_write_byte(SCNTL1,(t | CRST));
  379. udelay(50);
  380. scsi_write_byte(SCNTL1,t);
  381. puts("waiting for devices to spin up");
  382. for(i=0;i<end;i++) {
  383. udelay(1000); /* give the devices time to spin up */
  384. if (i % 1000 == 0)
  385. putc('.');
  386. }
  387. putc('\n');
  388. scsi_chip_init(); /* reinit the chip ...*/
  389. }
  390. void scsi_int_enable(void)
  391. {
  392. scsi_write_byte(SIEN,(unsigned char)scsi_int_mask);
  393. scsi_write_byte(SIEN+1,(unsigned char)(scsi_int_mask>>8));
  394. scsi_write_byte(DIEN,script_int_mask);
  395. }
  396. void scsi_write_dsp(unsigned long start)
  397. {
  398. #ifdef SCSI_SINGLE_STEP
  399. unsigned char t;
  400. #endif
  401. out32r(scsi_mem_addr + DSP,start);
  402. #ifdef SCSI_SINGLE_STEP
  403. t=scsi_read_byte(DCNTL);
  404. t|=STD;
  405. scsi_write_byte(DCNTL,t);
  406. #endif
  407. }
  408. /* only used for debug purposes */
  409. void scsi_print_script(void)
  410. {
  411. printf("script_select @ 0x%08lX\n",(unsigned long)&script_select[0]);
  412. printf("script_msgout @ 0x%08lX\n",(unsigned long)&script_msgout[0]);
  413. printf("script_msgin @ 0x%08lX\n",(unsigned long)&script_msgin[0]);
  414. printf("script_msgext @ 0x%08lX\n",(unsigned long)&script_msg_ext[0]);
  415. printf("script_cmd @ 0x%08lX\n",(unsigned long)&script_cmd[0]);
  416. printf("script_data_in @ 0x%08lX\n",(unsigned long)&script_data_in[0]);
  417. printf("script_data_out @ 0x%08lX\n",(unsigned long)&script_data_out[0]);
  418. printf("script_status @ 0x%08lX\n",(unsigned long)&script_status[0]);
  419. printf("script_complete @ 0x%08lX\n",(unsigned long)&script_complete[0]);
  420. printf("script_error @ 0x%08lX\n",(unsigned long)&script_error[0]);
  421. }
  422. void scsi_set_script(ccb *pccb)
  423. {
  424. int busdevfunc = pccb->priv;
  425. int i;
  426. i=0;
  427. script_select[i++]=swap_script(SCR_REG_REG(GPREG, SCR_AND, 0xfe));
  428. script_select[i++]=0; /* LED ON */
  429. script_select[i++]=swap_script(SCR_CLR(SCR_TRG)); /* select initiator mode */
  430. script_select[i++]=0;
  431. /* script_select[i++]=swap_script(SCR_SEL_ABS_ATN | pccb->target << 16); */
  432. script_select[i++]=swap_script(SCR_SEL_ABS | pccb->target << 16);
  433. script_select[i++]=swap_script(phys_to_bus(&script_cmd[4])); /* error handling */
  434. script_select[i++]=swap_script(SCR_JUMP); /* next section */
  435. /* script_select[i++]=swap_script((unsigned long)&script_msgout[0]); */ /* message out */
  436. script_select[i++]=swap_script(phys_to_bus(&script_cmd[0])); /* command out */
  437. #ifdef SCSI_SINGLE_STEP
  438. start_script_select=(unsigned long)&script_select[0];
  439. len_script_select=i*4;
  440. #endif
  441. i=0;
  442. script_msgout[i++]=swap_script(SCR_INT ^ IFFALSE (WHEN (SCR_MSG_OUT)));
  443. script_msgout[i++]=SIR_SEL_ATN_NO_MSG_OUT;
  444. script_msgout[i++]=swap_script( SCR_MOVE_ABS(1) ^ SCR_MSG_OUT);
  445. script_msgout[i++]=swap_script(phys_to_bus(&pccb->msgout[0]));
  446. script_msgout[i++]=swap_script(SCR_JUMP ^ IFTRUE (WHEN (SCR_COMMAND))); /* if Command phase */
  447. script_msgout[i++]=swap_script(phys_to_bus(&script_cmd[0])); /* switch to command */
  448. script_msgout[i++]=swap_script(SCR_INT); /* interrupt if not */
  449. script_msgout[i++]=SIR_MSG_OUT_NO_CMD;
  450. #ifdef SCSI_SINGLE_STEP
  451. start_script_msgout=(unsigned long)&script_msgout[0];
  452. len_script_msgout=i*4;
  453. #endif
  454. i=0;
  455. script_cmd[i++]=swap_script(SCR_MOVE_ABS(pccb->cmdlen) ^ SCR_COMMAND);
  456. script_cmd[i++]=swap_script(phys_to_bus(&pccb->cmd[0]));
  457. script_cmd[i++]=swap_script(SCR_JUMP ^ IFTRUE (WHEN (SCR_MSG_IN))); /* message in ? */
  458. script_cmd[i++]=swap_script(phys_to_bus(&script_msgin[0]));
  459. script_cmd[i++]=swap_script(SCR_JUMP ^ IFTRUE (IF (SCR_DATA_OUT))); /* data out ? */
  460. script_cmd[i++]=swap_script(phys_to_bus(&script_data_out[0]));
  461. script_cmd[i++]=swap_script(SCR_JUMP ^ IFTRUE (IF (SCR_DATA_IN))); /* data in ? */
  462. script_cmd[i++]=swap_script(phys_to_bus(&script_data_in[0]));
  463. script_cmd[i++]=swap_script(SCR_JUMP ^ IFTRUE (IF (SCR_STATUS))); /* status ? */
  464. script_cmd[i++]=swap_script(phys_to_bus(&script_status[0]));
  465. script_cmd[i++]=swap_script(SCR_JUMP ^ IFTRUE (IF (SCR_COMMAND))); /* command ? */
  466. script_cmd[i++]=swap_script(phys_to_bus(&script_cmd[0]));
  467. script_cmd[i++]=swap_script(SCR_JUMP ^ IFTRUE (IF (SCR_MSG_OUT))); /* message out ? */
  468. script_cmd[i++]=swap_script(phys_to_bus(&script_msgout[0]));
  469. script_cmd[i++]=swap_script(SCR_JUMP ^ IFTRUE (IF (SCR_MSG_IN))); /* just for error handling message in ? */
  470. script_cmd[i++]=swap_script(phys_to_bus(&script_msgin[0]));
  471. script_cmd[i++]=swap_script(SCR_INT); /* interrupt if not */
  472. script_cmd[i++]=SIR_CMD_OUT_ILL_PH;
  473. #ifdef SCSI_SINGLE_STEP
  474. start_script_cmd=(unsigned long)&script_cmd[0];
  475. len_script_cmd=i*4;
  476. #endif
  477. i=0;
  478. script_data_out[i++]=swap_script(SCR_MOVE_ABS(pccb->datalen)^ SCR_DATA_OUT); /* move */
  479. script_data_out[i++]=swap_script(phys_to_bus(pccb->pdata)); /* pointer to buffer */
  480. script_data_out[i++]=swap_script(SCR_JUMP ^ IFTRUE (WHEN (SCR_STATUS)));
  481. script_data_out[i++]=swap_script(phys_to_bus(&script_status[0]));
  482. script_data_out[i++]=swap_script(SCR_INT);
  483. script_data_out[i++]=SIR_DATA_OUT_ERR;
  484. #ifdef SCSI_SINGLE_STEP
  485. start_script_data_out=(unsigned long)&script_data_out[0];
  486. len_script_data_out=i*4;
  487. #endif
  488. i=0;
  489. script_data_in[i++]=swap_script(SCR_MOVE_ABS(pccb->datalen)^ SCR_DATA_IN); /* move */
  490. script_data_in[i++]=swap_script(phys_to_bus(pccb->pdata)); /* pointer to buffer */
  491. script_data_in[i++]=swap_script(SCR_JUMP ^ IFTRUE (WHEN (SCR_STATUS)));
  492. script_data_in[i++]=swap_script(phys_to_bus(&script_status[0]));
  493. script_data_in[i++]=swap_script(SCR_INT);
  494. script_data_in[i++]=SIR_DATA_IN_ERR;
  495. #ifdef SCSI_SINGLE_STEP
  496. start_script_data_in=(unsigned long)&script_data_in[0];
  497. len_script_data_in=i*4;
  498. #endif
  499. i=0;
  500. script_msgin[i++]=swap_script(SCR_MOVE_ABS (1) ^ SCR_MSG_IN);
  501. script_msgin[i++]=swap_script(phys_to_bus(&pccb->msgin[0]));
  502. script_msgin[i++]=swap_script(SCR_JUMP ^ IFTRUE (DATA (M_COMPLETE)));
  503. script_msgin[i++]=swap_script(phys_to_bus(&script_complete[0]));
  504. script_msgin[i++]=swap_script(SCR_JUMP ^ IFTRUE (DATA (M_DISCONNECT)));
  505. script_msgin[i++]=swap_script(phys_to_bus(&script_complete[0]));
  506. script_msgin[i++]=swap_script(SCR_JUMP ^ IFTRUE (DATA (M_SAVE_DP)));
  507. script_msgin[i++]=swap_script(phys_to_bus(&script_complete[0]));
  508. script_msgin[i++]=swap_script(SCR_JUMP ^ IFTRUE (DATA (M_RESTORE_DP)));
  509. script_msgin[i++]=swap_script(phys_to_bus(&script_complete[0]));
  510. script_msgin[i++]=swap_script(SCR_JUMP ^ IFTRUE (DATA (M_EXTENDED)));
  511. script_msgin[i++]=swap_script(phys_to_bus(&script_msg_ext[0]));
  512. script_msgin[i++]=swap_script(SCR_INT);
  513. script_msgin[i++]=SIR_MSG_RECEIVED;
  514. #ifdef SCSI_SINGLE_STEP
  515. start_script_msgin=(unsigned long)&script_msgin[0];
  516. len_script_msgin=i*4;
  517. #endif
  518. i=0;
  519. script_msg_ext[i++]=swap_script(SCR_CLR (SCR_ACK)); /* clear ACK */
  520. script_msg_ext[i++]=0;
  521. script_msg_ext[i++]=swap_script(SCR_MOVE_ABS (1) ^ SCR_MSG_IN); /* assuming this is the msg length */
  522. script_msg_ext[i++]=swap_script(phys_to_bus(&pccb->msgin[1]));
  523. script_msg_ext[i++]=swap_script(SCR_JUMP ^ IFFALSE (IF (SCR_MSG_IN)));
  524. script_msg_ext[i++]=swap_script(phys_to_bus(&script_complete[0])); /* no more bytes */
  525. script_msg_ext[i++]=swap_script(SCR_MOVE_ABS (1) ^ SCR_MSG_IN); /* next */
  526. script_msg_ext[i++]=swap_script(phys_to_bus(&pccb->msgin[2]));
  527. script_msg_ext[i++]=swap_script(SCR_JUMP ^ IFFALSE (IF (SCR_MSG_IN)));
  528. script_msg_ext[i++]=swap_script(phys_to_bus(&script_complete[0])); /* no more bytes */
  529. script_msg_ext[i++]=swap_script(SCR_MOVE_ABS (1) ^ SCR_MSG_IN); /* next */
  530. script_msg_ext[i++]=swap_script(phys_to_bus(&pccb->msgin[3]));
  531. script_msg_ext[i++]=swap_script(SCR_JUMP ^ IFFALSE (IF (SCR_MSG_IN)));
  532. script_msg_ext[i++]=swap_script(phys_to_bus(&script_complete[0])); /* no more bytes */
  533. script_msg_ext[i++]=swap_script(SCR_MOVE_ABS (1) ^ SCR_MSG_IN); /* next */
  534. script_msg_ext[i++]=swap_script(phys_to_bus(&pccb->msgin[4]));
  535. script_msg_ext[i++]=swap_script(SCR_JUMP ^ IFFALSE (IF (SCR_MSG_IN)));
  536. script_msg_ext[i++]=swap_script(phys_to_bus(&script_complete[0])); /* no more bytes */
  537. script_msg_ext[i++]=swap_script(SCR_MOVE_ABS (1) ^ SCR_MSG_IN); /* next */
  538. script_msg_ext[i++]=swap_script(phys_to_bus(&pccb->msgin[5]));
  539. script_msg_ext[i++]=swap_script(SCR_JUMP ^ IFFALSE (IF (SCR_MSG_IN)));
  540. script_msg_ext[i++]=swap_script(phys_to_bus(&script_complete[0])); /* no more bytes */
  541. script_msg_ext[i++]=swap_script(SCR_MOVE_ABS (1) ^ SCR_MSG_IN); /* next */
  542. script_msg_ext[i++]=swap_script(phys_to_bus(&pccb->msgin[6]));
  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[7]));
  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_INT);
  550. script_msg_ext[i++]=SIR_MSG_OVER7;
  551. #ifdef SCSI_SINGLE_STEP
  552. start_script_msg_ext=(unsigned long)&script_msg_ext[0];
  553. len_script_msg_ext=i*4;
  554. #endif
  555. i=0;
  556. script_status[i++]=swap_script(SCR_MOVE_ABS (1) ^ SCR_STATUS);
  557. script_status[i++]=swap_script(phys_to_bus(&pccb->status));
  558. script_status[i++]=swap_script(SCR_JUMP ^ IFTRUE (WHEN (SCR_MSG_IN)));
  559. script_status[i++]=swap_script(phys_to_bus(&script_msgin[0]));
  560. script_status[i++]=swap_script(SCR_INT);
  561. script_status[i++]=SIR_STATUS_ILL_PH;
  562. #ifdef SCSI_SINGLE_STEP
  563. start_script_status=(unsigned long)&script_status[0];
  564. len_script_status=i*4;
  565. #endif
  566. i=0;
  567. script_complete[i++]=swap_script(SCR_REG_REG (SCNTL2, SCR_AND, 0x7f));
  568. script_complete[i++]=0;
  569. script_complete[i++]=swap_script(SCR_CLR (SCR_ACK|SCR_ATN));
  570. script_complete[i++]=0;
  571. script_complete[i++]=swap_script(SCR_WAIT_DISC);
  572. script_complete[i++]=0;
  573. script_complete[i++]=swap_script(SCR_REG_REG(GPREG, SCR_OR, 0x01));
  574. script_complete[i++]=0; /* LED OFF */
  575. script_complete[i++]=swap_script(SCR_INT);
  576. script_complete[i++]=SIR_COMPLETE;
  577. #ifdef SCSI_SINGLE_STEP
  578. start_script_complete=(unsigned long)&script_complete[0];
  579. len_script_complete=i*4;
  580. #endif
  581. i=0;
  582. script_error[i++]=swap_script(SCR_INT); /* interrupt if error */
  583. script_error[i++]=SIR_SCRIPT_ERROR;
  584. #ifdef SCSI_SINGLE_STEP
  585. start_script_error=(unsigned long)&script_error[0];
  586. len_script_error=i*4;
  587. #endif
  588. i=0;
  589. script_reselection[i++]=swap_script(SCR_CLR (SCR_TRG)); /* target status */
  590. script_reselection[i++]=0;
  591. script_reselection[i++]=swap_script(SCR_WAIT_RESEL);
  592. script_reselection[i++]=swap_script(phys_to_bus(&script_select[0])); /* len = 4 */
  593. #ifdef SCSI_SINGLE_STEP
  594. start_script_reselection=(unsigned long)&script_reselection[0];
  595. len_script_reselection=i*4;
  596. #endif
  597. }
  598. void scsi_issue(ccb *pccb)
  599. {
  600. int busdevfunc = pccb->priv;
  601. int i;
  602. unsigned short sstat;
  603. int retrycnt; /* retry counter */
  604. for(i=0;i<3;i++)
  605. int_stat[i]=0; /* delete all int status */
  606. /* struct pccb must be set-up correctly */
  607. retrycnt=0;
  608. PRINTF("ID %d issue cmd %02X\n",pccb->target,pccb->cmd[0]);
  609. pccb->trans_bytes=0; /* no bytes transferred yet */
  610. scsi_set_script(pccb); /* fill in SCRIPT */
  611. scsi_int_mask=STO | UDC | MA; /* | CMP; / * Interrupts which are enabled */
  612. script_int_mask=0xff; /* enable all Ints */
  613. scsi_int_enable();
  614. scsi_write_dsp(phys_to_bus(&script_select[0])); /* start script */
  615. /* now we have to wait for IRQs */
  616. retry:
  617. /*
  618. * This version of the driver is _not_ interrupt driven,
  619. * but polls the chip's interrupt registers (ISTAT, DSTAT).
  620. */
  621. while(int_stat[0]==0)
  622. handle_scsi_int();
  623. if(int_stat[0]==SIR_COMPLETE) {
  624. if(pccb->msgin[0]==M_DISCONNECT) {
  625. PRINTF("Wait for reselection\n");
  626. for(i=0;i<3;i++)
  627. int_stat[i]=0; /* delete all int status */
  628. scsi_write_dsp(phys_to_bus(&script_reselection[0])); /* start reselection script */
  629. goto retry;
  630. }
  631. pccb->contr_stat=SIR_COMPLETE;
  632. return;
  633. }
  634. if((int_stat[0] & SCSI_INT_STATE)==SCSI_INT_STATE) { /* scsi interrupt */
  635. sstat=(unsigned short)int_stat[0];
  636. if((sstat & STO)==STO) { /* selection timeout */
  637. pccb->contr_stat=SCSI_SEL_TIME_OUT;
  638. scsi_write_byte(GPREG,0x01);
  639. PRINTF("ID: %X Selection Timeout\n",pccb->target);
  640. return;
  641. }
  642. if((sstat & UDC)==UDC) { /* unexpected disconnect */
  643. pccb->contr_stat=SCSI_UNEXP_DIS;
  644. scsi_write_byte(GPREG,0x01);
  645. PRINTF("ID: %X Unexpected Disconnect\n",pccb->target);
  646. return;
  647. }
  648. if((sstat & RSL)==RSL) { /* reselection */
  649. pccb->contr_stat=SCSI_UNEXP_DIS;
  650. scsi_write_byte(GPREG,0x01);
  651. PRINTF("ID: %X Unexpected Disconnect\n",pccb->target);
  652. return;
  653. }
  654. if(((sstat & MA)==MA)||((sstat & HTH)==HTH)) { /* phase missmatch */
  655. if(retrycnt<SCSI_MAX_RETRY) {
  656. pccb->trans_bytes=pccb->datalen -
  657. ((unsigned long)scsi_read_byte(DBC) |
  658. ((unsigned long)scsi_read_byte(DBC+1)<<8) |
  659. ((unsigned long)scsi_read_byte(DBC+2)<<16));
  660. for(i=0;i<3;i++)
  661. int_stat[i]=0; /* delete all int status */
  662. retrycnt++;
  663. PRINTF("ID: %X Phase Missmatch Retry %d Phase %02X transferred %lx\n",
  664. pccb->target,retrycnt,scsi_read_byte(SBCL),pccb->trans_bytes);
  665. scsi_write_dsp(phys_to_bus(&script_cmd[4])); /* start retry script */
  666. goto retry;
  667. }
  668. if((sstat & MA)==MA)
  669. pccb->contr_stat=SCSI_MA_TIME_OUT;
  670. else
  671. pccb->contr_stat=SCSI_HNS_TIME_OUT;
  672. PRINTF("Phase Missmatch stat %lx\n",pccb->contr_stat);
  673. return;
  674. } /* no phase int */
  675. /* if((sstat & CMP)==CMP) {
  676. pccb->contr_stat=SIR_COMPLETE;
  677. return;
  678. }
  679. */
  680. PRINTF("SCSI INT %lX\n",int_stat[0]);
  681. pccb->contr_stat=int_stat[0];
  682. return;
  683. } /* end scsi int */
  684. PRINTF("SCRIPT INT %lX phase %02X\n",int_stat[0],scsi_read_byte(SBCL));
  685. pccb->contr_stat=int_stat[0];
  686. return;
  687. }
  688. int scsi_exec(ccb *pccb)
  689. {
  690. unsigned char tmpcmd[16],tmpstat;
  691. int i,retrycnt,t;
  692. unsigned long transbytes,datalen;
  693. unsigned char *tmpptr;
  694. retrycnt=0;
  695. retry:
  696. scsi_issue(pccb);
  697. if(pccb->contr_stat!=SIR_COMPLETE)
  698. return false;
  699. if(pccb->status==S_GOOD)
  700. return true;
  701. if(pccb->status==S_CHECK_COND) { /* check condition */
  702. for(i=0;i<16;i++)
  703. tmpcmd[i]=pccb->cmd[i];
  704. pccb->cmd[0]=SCSI_REQ_SENSE;
  705. pccb->cmd[1]=pccb->lun<<5;
  706. pccb->cmd[2]=0;
  707. pccb->cmd[3]=0;
  708. pccb->cmd[4]=14;
  709. pccb->cmd[5]=0;
  710. pccb->cmdlen=6;
  711. pccb->msgout[0]=SCSI_IDENTIFY;
  712. transbytes=pccb->trans_bytes;
  713. tmpptr=pccb->pdata;
  714. pccb->pdata = &pccb->sense_buf[0];
  715. datalen=pccb->datalen;
  716. pccb->datalen=14;
  717. tmpstat=pccb->status;
  718. scsi_issue(pccb);
  719. for(i=0;i<16;i++)
  720. pccb->cmd[i]=tmpcmd[i];
  721. pccb->trans_bytes=transbytes;
  722. pccb->pdata=tmpptr;
  723. pccb->datalen=datalen;
  724. pccb->status=tmpstat;
  725. PRINTF("Request_sense sense key %x ASC %x ASCQ %x\n",pccb->sense_buf[2]&0x0f,
  726. pccb->sense_buf[12],pccb->sense_buf[13]);
  727. switch(pccb->sense_buf[2]&0xf) {
  728. case SENSE_NO_SENSE:
  729. case SENSE_RECOVERED_ERROR:
  730. /* seems to be ok */
  731. return true;
  732. break;
  733. case SENSE_NOT_READY:
  734. if((pccb->sense_buf[12]!=0x04)||(pccb->sense_buf[13]!=0x01)) {
  735. /* if device is not in process of becoming ready */
  736. return false;
  737. break;
  738. } /* else fall through */
  739. case SENSE_UNIT_ATTENTION:
  740. if(retrycnt<SCSI_MAX_RETRY_NOT_READY) {
  741. PRINTF("Target %d not ready, retry %d\n",pccb->target,retrycnt);
  742. for(t=0;t<SCSI_NOT_READY_TIME_OUT;t++)
  743. udelay(1000); /* 1sec wait */
  744. retrycnt++;
  745. goto retry;
  746. }
  747. PRINTF("Target %d not ready, %d retried\n",pccb->target,retrycnt);
  748. return false;
  749. default:
  750. return false;
  751. }
  752. }
  753. PRINTF("Status = %X\n",pccb->status);
  754. return false;
  755. }
  756. void scsi_chip_init(void)
  757. {
  758. /* first we issue a soft reset */
  759. scsi_write_byte(ISTAT,SRST);
  760. udelay(1000);
  761. scsi_write_byte(ISTAT,0);
  762. /* setup chip */
  763. scsi_write_byte(SCNTL0,0xC0); /* full arbitration no start, no message, parity disabled, master */
  764. scsi_write_byte(SCNTL1,0x00);
  765. scsi_write_byte(SCNTL2,0x00);
  766. #ifndef CONFIG_SYS_SCSI_SYM53C8XX_CCF /* config value for none 40 MHz clocks */
  767. scsi_write_byte(SCNTL3,0x13); /* synchronous clock 40/4=10MHz, asynchronous 40MHz */
  768. #else
  769. scsi_write_byte(SCNTL3,CONFIG_SYS_SCSI_SYM53C8XX_CCF); /* config value for none 40 MHz clocks */
  770. #endif
  771. scsi_write_byte(SCID,0x47); /* ID=7, enable reselection */
  772. scsi_write_byte(SXFER,0x00); /* synchronous transfer period 10MHz, asynchronous */
  773. scsi_write_byte(SDID,0x00); /* targed SCSI ID = 0 */
  774. scsi_int_mask=0x0000; /* no Interrupt is enabled */
  775. script_int_mask=0x00;
  776. scsi_int_enable();
  777. scsi_write_byte(GPREG,0x01); /* GPIO0 is LED (off) */
  778. scsi_write_byte(GPCNTL,0x0E); /* GPIO0 is Output */
  779. scsi_write_byte(STIME0,0x08); /* handshake timer disabled, selection timeout 512msec */
  780. scsi_write_byte(RESPID,0x80); /* repond only to the own ID (reselection) */
  781. scsi_write_byte(STEST1,0x00); /* not isolated, SCLK is used */
  782. scsi_write_byte(STEST2,0x00); /* no Lowlevel Mode? */
  783. scsi_write_byte(STEST3,0x80); /* enable tolerANT */
  784. scsi_write_byte(CTEST3,0x04); /* clear FIFO */
  785. scsi_write_byte(CTEST4,0x00);
  786. scsi_write_byte(CTEST5,0x00);
  787. #ifdef SCSI_SINGLE_STEP
  788. /* scsi_write_byte(DCNTL,IRQM | SSM); */
  789. scsi_write_byte(DCNTL,IRQD | SSM);
  790. scsi_write_byte(DMODE,MAN);
  791. #else
  792. /* scsi_write_byte(DCNTL,IRQM); */
  793. scsi_write_byte(DCNTL,IRQD);
  794. scsi_write_byte(DMODE,0x00);
  795. #endif
  796. }
  797. #endif