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