sym53c8xx.c 29 KB

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