sym53c8xx.c 29 KB

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