sym53c8xx.c 29 KB

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