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