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