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