]> rtime.felk.cvut.cz Git - lincan.git/blob - lincan/src/c_can.c
e51181b0233c778678676b27ca6b2e87647f96f6
[lincan.git] / lincan / src / c_can.c
1 /* c_can.c - Hynix HMS30c7202 ARM generic C_CAN module handling
2  * Linux CAN-bus device driver.
3  * Written by Sebastian Stolzenberg email:stolzi@sebastian-stolzenberg.de
4  * Based on code from Arnaud Westenberg email:arnaud@wanadoo.nl
5  * and Ake Hedman, eurosource, akhe@eurosource.se
6  * Rewritten for new CAN queues by Pavel Pisa - OCERA team member
7  * email:pisa@cmp.felk.cvut.cz
8  * This software is released under the GPL-License.
9  * Version lincan-0.3  17 Jun 2004
10  */
11
12 #define __NO_VERSION__
13
14 #include "../include/can.h"
15 #include "../include/can_sysdep.h"
16 #include "../include/main.h"
17 #include "../include/c_can.h"
18
19 extern int stdmask;
20 extern int extmask;
21
22 can_spinlock_t c_can_spwlock=SPIN_LOCK_UNLOCKED; // Spin lock for write operations
23 can_spinlock_t c_can_sprlock=SPIN_LOCK_UNLOCKED; // Spin lock for read operations
24 can_spinlock_t c_can_if1lock=SPIN_LOCK_UNLOCKED; // spin lock for the if1 register
25 can_spinlock_t c_can_if2lock=SPIN_LOCK_UNLOCKED; // spin lcok for the if2 register
26
27 /**
28  * c_can_enable_configuration - enable chip configuration mode
29  * @pchip: pointer to chip state structure
30  */
31 int c_can_enable_configuration(struct canchip_t *pchip)
32 {
33    int i=0;
34    u16 flags;
35    DEBUGMSG("(c%d)calling c_can_enable_configuration(...)\n", pchip->chip_idx);
36 /*
37    DEBUGMSG("Trying disable_irq(...) : ");
38    //disable IRQ
39         disable_irq(chip->chip_irq);
40 */
41    //read Control Register
42    flags=c_can_read_reg_w(pchip, CCCR);
43    //set Init-Bit in the Control Register (10 tries)
44    while ((!(flags & CR_INIT)) && (i<=10))
45      {
46         c_can_write_reg_w(pchip,flags|CR_INIT, CCCR);
47         udelay(1000);
48         i++;
49         flags=c_can_read_reg_w(pchip, CCCR);
50      }
51    if (i>=10)
52      {
53         CANMSG("Reset error\n");
54         //enable_irq(chip->chip_irq);
55         return -ENODEV;
56      }
57
58    DEBUGMSG("-> ok\n");
59    return 0;
60 }
61
62 ///////////////////////////////////////////////////////////////////////
63 int c_can_disable_configuration(struct canchip_t *pchip)
64 {
65    int i=0;
66    u16 flags;
67
68    DEBUGMSG("(c%d)calling c_can_disable_configuration(...)\n", pchip->chip_idx);
69    //read Control Register
70    flags=c_can_read_reg_w(pchip, CCCR);
71
72    //reset Init-Bit in the Control Register (10 tries)
73    while ( (flags & CR_INIT) && (i<=10) )
74      {
75         c_can_write_reg_w( pchip,flags & ~CR_INIT, CCCR);
76         udelay(1000); //100 microseconds
77         i++;
78         flags=c_can_read_reg_w(pchip, CCCR);
79      }
80    if (i>=10)
81      {
82         CANMSG("Error leaving reset status\n");
83         return -ENODEV;
84      }
85
86    //enable IRQ
87    //enable_irq(chip->chip_irq);
88    DEBUGMSG("-> ok\n");
89    return 0;
90 }
91
92 ///////////////////////////////////////////////////////////////////////
93 int c_can_chip_config(struct canchip_t *pchip)
94 {
95
96    DEBUGMSG("(c%d)calling c_can_chip_config(...)\n", pchip->chip_idx);
97    // Validate pointer
98    if ( NULL == pchip ) return -1;
99
100    if (pchip->baudrate == 0)
101      pchip->baudrate=1000;
102
103    if (c_can_baud_rate(pchip,pchip->baudrate*1000,pchip->clock,0,75,0))
104      {
105         CANMSG("Error configuring baud rate\n");
106         return -ENODEV;
107      }
108         /*if (extended){
109            if (c_can_extended_mask(pchip,0x0000000,extmask)) {
110                    CANMSG("Error configuring extended mask\n");
111                    return -ENODEV;
112       }
113    }else{
114       if (c_can_standard_mask(pchip,0x0000,stdmask)) {
115                    CANMSG("Error configuring standard mask\n");
116                    return -ENODEV;
117            }
118         }*/
119    if (c_can_clear_objects(pchip))
120      {
121         CANMSG("Error clearing message objects\n");
122         return -ENODEV;
123      }
124    if (c_can_config_irqs(pchip, CR_MIE | CR_SIE | CR_EIE))
125      {
126         CANMSG("Error configuring interrupts\n");
127         return -ENODEV;
128      }
129
130    DEBUGMSG("-> Configured successfully\n");
131
132 #ifdef REGDUMP
133    c_can_registerdump(pchip);
134 #endif
135
136    return 0;
137 }
138
139 ///////////////////////////////////////////////////////////////////////
140 /*
141  * Checks if the Busy-Bit in the IF1-Command-Request Register is set
142  */
143 int c_can_if1_busycheck(struct canchip_t *pchip)
144 {
145
146    int i=0;
147    unsigned short comreg = 0;
148
149    comreg = c_can_read_reg_w( pchip, CCIF1CR);
150    while ( (comreg & IFXCR_BUSY) && (i<=10) )
151      {
152         udelay(100); //100 microseconds
153         i++;
154         comreg=c_can_read_reg_w( pchip, CCIF1CR);
155      }
156    if (i>=10)
157      {
158         CANMSG("Error Busy-Bit stays set\n");
159         return -ENODEV;
160      }
161
162    return 0;
163 }
164
165 ///////////////////////////////////////////////////////////////////////
166 /*
167  * Checks if the Busy-Bit in the IF2-Command-Request Register is set
168  */
169 int c_can_if2_busycheck(struct canchip_t *pchip)
170 {
171
172    int i=0;
173    unsigned short comreg = 0;
174
175    comreg = c_can_read_reg_w( pchip, CCIF2CR);
176    while ( (comreg & IFXCR_BUSY) && (i<=10) )
177      {
178         udelay(100); //100 microseconds
179         i++;
180         comreg=c_can_read_reg_w( pchip, CCIF2CR);
181      }
182    if (i>=10)
183      {
184         CANMSG("Error Busy-Bit stays set\n");
185         return -ENODEV;
186      }
187
188    return 0;
189 }
190
191 ///////////////////////////////////////////////////////////////////////
192 /*
193  * Though the C-CAN Chip can handle one mask for each Message Object, this Method defines
194  * one mask for all MOs. That means every MO gets the same mask.
195  */
196
197 /* Set communication parameters.
198  * param rate baud rate in Hz
199  * param clock frequency of C-CAN clock in Hz
200  * param sjw synchronization jump width (0-3) prescaled clock cycles
201  * param sampl_pt sample point in % (0-100) sets (TSEG1+2)/(TSEG1+TSEG2+3) ratio
202  * param flags fields BTR1_SAM, OCMODE, OCPOL, OCTP, OCTN, CLK_OFF, CBP
203  */
204 int c_can_baud_rate(struct canchip_t *pchip, int rate, int clock,
205                         int sjw, int sampl_pt, int flags)
206 {
207    int best_error = 1000000000, error;
208    int best_tseg=0, best_brp=0, best_rate=0, brp=0;
209    int tseg=0, tseg1=0, tseg2=0;
210
211    unsigned short tempCR = 0;
212
213    DEBUGMSG("(c%d)calling c_can_baud_rate(...)\n", pchip->chip_idx);
214
215    if (c_can_enable_configuration(pchip))
216      return -ENODEV;
217
218         /* tseg even = round down, odd = round up */
219    for (tseg=(0+0+2)*2; tseg<=(MAX_TSEG2+MAX_TSEG1+2)*2+1; tseg++)
220      {
221         brp = clock/((1+tseg/2)*rate)+tseg%2;
222         if (brp == 0 || brp > 64)
223           continue;
224         error = rate - clock/(brp*(1+tseg/2));
225         if (error < 0)
226           error = -error;
227         if (error <= best_error)
228           {
229              best_error = error;
230              best_tseg = tseg/2;
231              best_brp = brp-1;
232              best_rate = clock/(brp*(1+tseg/2));
233           }
234      }
235    if (best_error && (rate/best_error < 10))
236      {
237         CANMSG("baud rate %d is not possible with %d Hz clock\n",
238                rate, clock);
239         CANMSG("%d bps. brp=%d, best_tseg=%d, tseg1=%d, tseg2=%d\n",
240                best_rate, best_brp, best_tseg, tseg1, tseg2);
241         return -EINVAL;
242      }
243    tseg2 = best_tseg-(sampl_pt*(best_tseg+1))/100;
244    if (tseg2 < 0)
245      tseg2 = 0;
246    if (tseg2 > MAX_TSEG2)
247      tseg2 = MAX_TSEG2;
248    tseg1 = best_tseg-tseg2-2;
249    if (tseg1 > MAX_TSEG1)
250      {
251         tseg1 = MAX_TSEG1;
252         tseg2 = best_tseg-tseg1-2;
253      }
254
255    DEBUGMSG("-> Setting %d bps.\n", best_rate);
256    DEBUGMSG("->brp=%d, best_tseg=%d, tseg1=%d, tseg2=%d, sampl_pt=%d\n",
257             best_brp, best_tseg, tseg1, tseg2,
258             (100*(best_tseg-tseg2)/(best_tseg+1)));
259
260    //read Control Register
261    tempCR = c_can_read_reg_w( pchip, CCCR);
262    //Configuration Change Enable
263    c_can_write_reg_w(pchip, tempCR | CR_CCE, CCCR);
264    c_can_write_reg_w(pchip, ((unsigned short)tseg2)<<12 | ((unsigned short)tseg1)<<8
265                          | (unsigned short)sjw<<6 | (unsigned short) best_brp,
266                          CCBT);
267
268    if (c_can_disable_configuration(pchip))
269      return -ENODEV;
270
271    return 0;
272 }
273
274 ///////////////////////////////////////////////////////////////////////
275 int c_can_mask(struct msgobj_t *pmsgobj,
276                u32 mask,
277                u16 usedirbit)
278 {
279    unsigned short tempreg = 0;
280    unsigned short readMaskCM;
281    unsigned short writeMaskCM;
282
283    DEBUGMSG("(c%dm%d)calling c_can_mask(...)\n", pmsgobj->hostchip->chip_idx, pmsgobj->object);
284
285    readMaskCM = IFXCM_CNTRL | IFXCM_ARB | IFXCM_MASK;
286    writeMaskCM = IFXCM_CNTRL | IFXCM_ARB | IFXCM_MASK | IFXCM_WRRD;
287
288    spin_lock( &c_can_if1lock );
289
290    //load Message Object in IF1
291    if (c_can_if1_busycheck(pmsgobj->hostchip)) return -ENODEV;
292    c_can_write_reg_w(pmsgobj->hostchip, readMaskCM, CCIF1CM);
293    c_can_write_reg_w(pmsgobj->hostchip, pmsgobj->object, CCIF1CR);
294
295    //setting Message Valid Bit to zero
296    if (c_can_if1_busycheck(pmsgobj->hostchip)) return -ENODEV;
297    tempreg = c_can_read_reg_w(pmsgobj->hostchip, CCIF1A2);
298    c_can_write_reg_w(pmsgobj->hostchip, tempreg & ~(IFXARB2_MVAL), CCIF1A2);
299    c_can_write_reg_w(pmsgobj->hostchip, writeMaskCM, CCIF1CM);
300    c_can_write_reg_w(pmsgobj->hostchip, pmsgobj->object, CCIF1CR);
301
302    //setting UMask, MsgVal and Mask Register
303    if (c_can_if1_busycheck(pmsgobj->hostchip)) return -ENODEV;
304
305    //writing acceptance mask for extended or standart mode
306    if (can_msgobj_test_fl(pmsgobj,RX_MODE_EXT))
307      {
308         if (usedirbit)
309           c_can_write_reg_w(pmsgobj->hostchip, 
310                             (mask>>16 & 0x1FFF) | IFXMSK2_MXTD | IFXMSK2_MDIR, CCIF1M2);
311         else
312           c_can_write_reg_w(pmsgobj->hostchip, 
313                             (mask>>16 & 0x1FFF) | IFXMSK2_MXTD, CCIF1M2);
314         c_can_write_reg_w(pmsgobj->hostchip, (mask & 0xFFFF), CCIF1M1);
315      }
316    else
317      {
318         if (usedirbit)
319           c_can_write_reg_w(pmsgobj->hostchip, 
320                             ((mask<<2) & 0x1FFC) | IFXMSK2_MDIR, CCIF1M2);
321         else
322           c_can_write_reg_w(pmsgobj->hostchip,
323                             ((mask<<2) & 0x1FFC), CCIF1M2);
324         c_can_write_reg_w(pmsgobj->hostchip, 0, CCIF1M1);
325      }
326    //seting Message Valid Bit to one
327    tempreg = c_can_read_reg_w(pmsgobj->hostchip, CCIF1A2);
328    c_can_write_reg_w(pmsgobj->hostchip, tempreg | IFXARB2_MVAL, CCIF1A2);
329    //write to chip
330    c_can_write_reg_w(pmsgobj->hostchip, writeMaskCM, CCIF1CM);
331    c_can_write_reg_w(pmsgobj->hostchip, pmsgobj->object, CCIF1CR);
332
333    spin_unlock( &c_can_if1lock );
334
335    DEBUGMSG("-> Setting acceptance mask to 0x%lx\n",(unsigned long)mask);
336
337 #ifdef REGDUMP
338    c_can_registerdump(pmsgobj->hostchip);
339 #endif
340
341    return 0;
342 }
343
344 ///////////////////////////////////////////////////////////////////////
345 int c_can_use_mask(struct msgobj_t *pmsgobj,
346                    u16 useflag)
347 {
348    unsigned short tempreg = 0;
349    unsigned short readMaskCM;
350    unsigned short writeMaskCM;
351
352 #ifdef DEBUG
353    char *boolstring = "false";
354    if (useflag) boolstring = "true";
355 #endif
356    DEBUGMSG("(c%dm%d)calling c_can_use_mask(...)\n", pmsgobj->hostchip->chip_idx, pmsgobj->object);
357
358    readMaskCM = IFXCM_CNTRL | IFXCM_ARB;
359    writeMaskCM = IFXCM_CNTRL | IFXCM_ARB | IFXCM_WRRD;;
360
361    spin_lock( &c_can_if1lock );
362
363    //load Message Object in IF1
364    if (c_can_if1_busycheck(pmsgobj->hostchip)) return -ENODEV;
365    c_can_write_reg_w(pmsgobj->hostchip, readMaskCM, CCIF1CM);
366    c_can_write_reg_w(pmsgobj->hostchip, pmsgobj->object, CCIF1CR);
367
368    //setting Message Valid Bit to zero
369    if (c_can_if1_busycheck(pmsgobj->hostchip)) return -ENODEV;
370    tempreg = c_can_read_reg_w(pmsgobj->hostchip, CCIF1A2);
371    c_can_write_reg_w(pmsgobj->hostchip, tempreg & ~IFXARB2_MVAL, CCIF1A2);
372    c_can_write_reg_w(pmsgobj->hostchip, writeMaskCM, CCIF1CM);
373    c_can_write_reg_w(pmsgobj->hostchip, pmsgobj->object, CCIF1CR);
374
375    //setting UMask bit
376    if (c_can_if1_busycheck(pmsgobj->hostchip)) return -ENODEV;
377    if ( useflag )
378      {
379         tempreg = c_can_read_reg_w(pmsgobj->hostchip, CCIF1DMC);
380         c_can_write_reg_w(pmsgobj->hostchip, tempreg | IFXMC_UMASK, CCIF1DMC);
381      }
382    else
383      {
384         tempreg = c_can_read_reg_w(pmsgobj->hostchip, CCIF1DMC);
385         c_can_write_reg_w(pmsgobj->hostchip, tempreg & ~IFXMC_UMASK, CCIF1DMC);
386      }
387    //seting Message Valid Bit to one
388    tempreg = c_can_read_reg_w(pmsgobj->hostchip, CCIF1A2);
389    c_can_write_reg_w(pmsgobj->hostchip, tempreg | IFXARB2_MVAL, CCIF1A2);
390    //write to chip
391    c_can_write_reg_w(pmsgobj->hostchip, writeMaskCM, CCIF1CM);
392    c_can_write_reg_w(pmsgobj->hostchip, pmsgobj->object, CCIF1CR);
393
394    spin_unlock( &c_can_if1lock );
395
396 #ifdef DEBUG
397    DEBUGMSG("-> Setting umask bit to %s\n",boolstring);
398 #endif
399 #ifdef REGDUMP
400    c_can_registerdump(pmsgobj->hostchip);
401 #endif
402
403    return 0;
404 }
405
406 ///////////////////////////////////////////////////////////////////////
407 int c_can_clear_objects(struct canchip_t *pchip)
408 {
409    unsigned short i = 0;
410    unsigned short tempreg = 0;
411
412    unsigned short maskCM = IFXCM_ARB;
413
414    DEBUGMSG("(c%d)calling c_can_clear_objects(...)\n", pchip->chip_idx);
415
416    spin_lock( &c_can_if1lock );
417    spin_lock( &c_can_if2lock );
418
419    for (i=0; i<0x10; i++)
420      {
421
422         //loading Message Objects in IF1 and IF2
423         if (c_can_if1_busycheck(pchip)) return -ENODEV;
424         c_can_write_reg_w(pchip, maskCM, CCIF1CM);
425         c_can_write_reg_w(pchip, i, CCIF1CR);
426         if (c_can_if2_busycheck(pchip)) return -ENODEV;
427         c_can_write_reg_w(pchip, maskCM, CCIF2CM);
428         c_can_write_reg_w(pchip, i+0x10, CCIF2CR);
429
430         //setting Message Valid Bit to zero
431         if (c_can_if1_busycheck(pchip)) return -ENODEV;
432         tempreg = c_can_read_reg_w(pchip, CCIF1A2);
433         c_can_write_reg_w(pchip, tempreg & ~IFXARB2_MVAL, CCIF1A2);
434         c_can_write_reg_w(pchip, i, CCIF1CR);
435         if (c_can_if2_busycheck(pchip)) return -ENODEV;
436         tempreg = c_can_read_reg_w(pchip, CCIF2A2);
437         c_can_write_reg_w(pchip, tempreg & ~IFXARB2_MVAL, CCIF2A2);
438         c_can_write_reg_w(pchip, i+0x10, CCIF2CR);
439      }
440
441    for (i=0; i<pchip->max_objects; i++)
442      {
443         if (can_msgobj_test_fl(pchip->msgobj[i],OPENED))
444           {
445              // In- and output buffer re-initialization
446              canqueue_ends_flush_inlist(pchip->msgobj[i]->qends);
447              canqueue_ends_flush_outlist(pchip->msgobj[i]->qends);
448
449           }
450      }
451
452    spin_unlock( &c_can_if1lock );
453    spin_unlock( &c_can_if2lock );
454
455    DEBUGMSG("-> Message Objects reset\n");
456
457    return 0;
458 }
459
460 ///////////////////////////////////////////////////////////////////////
461 int c_can_config_irqs(struct canchip_t *pchip,
462                       u16 irqs)
463 {
464    u16 tempreg;
465
466    DEBUGMSG("(c%d)calling c_can_config_irqs(...)\n", pchip->chip_idx);
467
468    tempreg = c_can_read_reg_w(pchip, CCCR);
469    //DEBUGMSG("-> CAN Control Register: 0x%.4lx\n",(long)tempreg);
470    c_can_write_reg_w(pchip, tempreg | (irqs & 0xe), CCCR);
471    DEBUGMSG("-> Configured hardware interrupt delivery\n");
472    return 0;
473 }
474
475 ///////////////////////////////////////////////////////////////////////
476 int c_can_pre_read_config(struct canchip_t *pchip, struct msgobj_t *pmsgobj)
477 {
478    unsigned short readMaskCM = IFXCM_CNTRL | IFXCM_ARB;
479    unsigned short writeMaskCM = IFXCM_CNTRL | IFXCM_ARB | IFXCM_WRRD;
480    unsigned short mcreg = 0;
481    u32 id=pmsgobj->rx_preconfig_id;
482
483    DEBUGMSG("(c%dm%d)calling c_can_pre_read_config(...)\n", pmsgobj->hostchip->chip_idx, pmsgobj->object);
484
485    spin_lock( &c_can_if1lock );
486
487
488    //loading Message Object in IF1
489    if (c_can_if1_busycheck(pmsgobj->hostchip)) return -ENODEV;
490    c_can_write_reg_w(pmsgobj->hostchip, readMaskCM, CCIF1CM);
491    c_can_write_reg_w(pmsgobj->hostchip, pmsgobj->object, CCIF1CR);
492    //setting Message Valid Bit to zero
493    if (c_can_if1_busycheck(pmsgobj->hostchip)) return -ENODEV;
494    c_can_write_reg_w(pmsgobj->hostchip, 0, CCIF1A2);
495    c_can_write_reg_w(pmsgobj->hostchip, writeMaskCM, CCIF1CM);
496    c_can_write_reg_w(pmsgobj->hostchip, pmsgobj->object, CCIF1CR);
497    //Configuring Message-Object
498    if (c_can_if1_busycheck(pmsgobj->hostchip)) return -ENODEV;
499    mcreg = c_can_read_reg_w(pmsgobj->hostchip, CCIF1CM);
500    c_can_write_reg_w(pmsgobj->hostchip, 
501                      ((mcreg & IFXMC_UMASK) | IFXMC_EOB | IFXMC_RXIE), CCIF1DMC);
502    //writing arbitration mask for extended or standart mode
503    if (can_msgobj_test_fl(pmsgobj,RX_MODE_EXT))
504      {
505         c_can_write_reg_w(pmsgobj->hostchip, 
506                           IFXARB2_XTD | IFXARB2_MVAL | (id>>16 & 0x1FFF), CCIF1A2);
507         c_can_write_reg_w(pmsgobj->hostchip, id & 0xFFFF, CCIF1A1);
508      }
509    else
510      {
511         c_can_write_reg_w(pmsgobj->hostchip, 
512                           IFXARB2_MVAL | (id<<2 & 0x1FFC), CCIF1A2);
513         //c_can_write_reg_w(pmsgobj->hostchip, 0, CCIF1A1);
514      }
515    c_can_write_reg_w(pmsgobj->hostchip, writeMaskCM, CCIF1CM);
516    c_can_write_reg_w(pmsgobj->hostchip, pmsgobj->object, CCIF1CR);
517
518    spin_unlock( &c_can_if1lock );
519
520    DEBUGMSG("-> Receiving through message object %d with id=%d\n", pmsgobj->object,
521             id);
522 #ifdef REGDUMP
523    c_can_registerdump(pmsgobj->hostchip);
524 #endif
525
526    return 0;
527 }
528
529 ///////////////////////////////////////////////////////////////////////
530 int c_can_pre_write_config(struct canchip_t *chip, struct msgobj_t *obj, 
531                                 struct canmsg_t *msg)
532 {
533         return 0; 
534 }
535
536  ///////////////////////////////////////////////////////////////////////
537 /*
538  *Prepare the Chip to send specified Message over specified Messageobject
539  *In this version the method also sends the message.
540  */
541
542 int c_can_send_msg(struct canchip_t *pchip, struct msgobj_t *pmsgobj,
543                         struct canmsg_t *pmsg)
544 {   
545    unsigned short readMaskCM = IFXCM_CNTRL | IFXCM_ARB | IFXCM_DA | IFXCM_DB;
546    unsigned short writeMaskCM = IFXCM_CNTRL | IFXCM_ARB | IFXCM_DA | IFXCM_DB| IFXCM_WRRD;
547    unsigned short writeSendMskCM = IFXCM_CNTRL | IFXCM_ARB | IFXCM_DA | IFXCM_DB| IFXCM_WRRD | IFXCM_TRND;
548    unsigned short mcreg = 0;
549    //unsigned short arbreg = 0;
550    unsigned short dataA1 = 0;
551    unsigned short dataA2 = 0;
552    unsigned short dataB1 = 0;
553    unsigned short dataB2 = 0;
554
555    DEBUGMSG("(c%dm%d)calling c_can_send_msg(...)\n", pmsgobj->hostchip->chip_idx, pmsgobj->object);
556
557    spin_lock( &c_can_if2lock );
558
559    can_msgobj_clear_fl(pmsgobj,RX_MODE);
560    
561    //loading Message Object in IF1
562    if (c_can_if2_busycheck(pmsgobj->hostchip)) return -ENODEV;
563    c_can_write_reg_w(pmsgobj->hostchip, readMaskCM, CCIF2CM);
564    c_can_write_reg_w(pmsgobj->hostchip, pmsgobj->object, CCIF2CR);
565    //setting Message Valid Bit to zero
566    if (c_can_if2_busycheck(pmsgobj->hostchip)) return -ENODEV;
567    c_can_write_reg_w(pmsgobj->hostchip, 0, CCIF2A2);
568    c_can_write_reg_w(pmsgobj->hostchip, writeMaskCM, CCIF2CM);
569    c_can_write_reg_w(pmsgobj->hostchip, pmsgobj->object, CCIF2CR);
570    //Configuring MO
571    if (c_can_if2_busycheck(pmsgobj->hostchip)) return -ENODEV;
572    mcreg = c_can_read_reg_w( pmsgobj->hostchip, CCIF2CM);
573    //remote enable?
574    //define Command Mask
575    c_can_write_reg_w(pmsgobj->hostchip, (mcreg & IFXMC_UMASK) | IFXMC_EOB | IFXMC_TXIE
576                      | IFXMC_RMTEN | IFXMC_NEWDAT | IFXMC_TXRQST | (pmsg->length & 0xF), CCIF2DMC);
577    //set Arbitration Bits
578    if (can_msgobj_test_fl(pmsgobj,RX_MODE_EXT))
579      {
580         c_can_write_reg_w(pmsgobj->hostchip, (u16)(pmsg->id), CCIF2A1);
581         c_can_write_reg_w(pmsgobj->hostchip, IFXARB2_XTD | IFXARB2_MVAL | IFXARB2_DIR
582                                             | ((u16)(pmsg->id>>16) & 0x1FFF), CCIF2A2);
583      }
584    else
585      {
586         c_can_write_reg_w(pmsgobj->hostchip, 
587                           (IFXARB2_MVAL | IFXARB2_DIR | ((u16)(pmsg->id<<2) & 0x1FFC)), CCIF2A2);
588         c_can_write_reg_w(pmsgobj->hostchip, 0, CCIF1A1);
589      }
590    //write Data
591    if (pmsg->length>0)
592      {
593         dataA1 = pmsg->data[0] | (u16)pmsg->data[1]<<8;
594         dataA2 = pmsg->data[2] | (u16)pmsg->data[3]<<8;
595         dataB1 = pmsg->data[4] | (u16)pmsg->data[5]<<8;
596         dataB2 = pmsg->data[6] | (u16)pmsg->data[7]<<8;
597
598         c_can_write_reg_w(pmsgobj->hostchip, dataA1, CCIF2DA1);
599         c_can_write_reg_w(pmsgobj->hostchip, dataA2, CCIF2DA2);
600         c_can_write_reg_w(pmsgobj->hostchip, dataB1, CCIF2DB1);
601         c_can_write_reg_w(pmsgobj->hostchip, dataB2, CCIF2DB2);
602      }
603
604    c_can_write_reg_w(pmsgobj->hostchip, writeSendMskCM, CCIF2CM);
605    c_can_write_reg_w(pmsgobj->hostchip, pmsgobj->object, CCIF2CR);
606
607    spin_unlock( &c_can_if2lock );
608
609    DEBUGMSG("-> ok\n");
610 #ifdef REGDUMP
611    c_can_registerdump(pmsgobj->hostchip);
612 #endif
613
614    return 0;
615 }
616
617 //////////////////////////////////////////////////////////////////////
618 int c_can_remote_request(struct canchip_t *pchip, struct msgobj_t *pmsgobj )
619 {
620    unsigned short readMaskCM = IFXCM_CNTRL;// | IFXCM_ARB;
621    //unsigned short writeMaskCM = IFXCM_CNTRL | IFXCM_ARB | IFXCM_WRRD;
622    unsigned short mcreg = 0;
623
624    DEBUGMSG("(c%dm%d)calling c_can_remote_request(...)\n", pmsgobj->hostchip->chip_idx, pmsgobj->object);
625
626    //Remote request is only available when the message object is in receiving mode
627    if (!can_msgobj_test_fl(pmsgobj,RX_MODE))
628      {
629         return 1;
630      }
631
632    spin_lock( &c_can_if1lock );
633
634    //loading Message Object in IF1
635    if (c_can_if1_busycheck(pmsgobj->hostchip)) return -ENODEV;
636    c_can_write_reg_w(pmsgobj->hostchip, readMaskCM, CCIF1CM);
637    c_can_write_reg_w(pmsgobj->hostchip, pmsgobj->object, CCIF1CR);
638    //setting Transmit-Request-Bit
639    if (c_can_if1_busycheck(pmsgobj->hostchip)) return -ENODEV;
640    mcreg = c_can_read_reg_w( pmsgobj->hostchip, CCIF1DMC);
641    c_can_write_reg_w(pmsgobj->hostchip, mcreg | IFXMC_TXRQST, CCIF1DMC);
642
643    c_can_write_reg_w(pmsgobj->hostchip, pmsgobj->object, CCIF1CR);
644
645    spin_unlock( &c_can_if1lock );
646
647    DEBUGMSG("-> Sent remote request through message object %d\n", pmsgobj->object);
648 #ifdef REGDUMP
649    c_can_registerdump(pmsgobj->hostchip);
650 #endif
651
652    return 0;
653 }
654
655 ///////////////////////////////////////////////////////////////////////
656 int c_can_set_btregs(struct canchip_t *pchip,
657                      u16 btr0,
658                      u16 btr1)
659 {
660    unsigned short tempCR = 0;
661
662    DEBUGMSG("(c%d)calling c_can_set_btregs(...)\n", pchip->chip_idx);
663
664    // Validate pointer
665    if ( NULL == pchip ) return -1;
666
667    if (c_can_enable_configuration(pchip))
668      return -ENODEV;
669
670    //read Control Register
671    tempCR = c_can_read_reg_w(pchip, CCCR);
672    //Configuration Change Enable
673    c_can_write_reg_w(pchip, tempCR | CR_CCE, CCCR);
674    c_can_write_reg_w(pchip, btr0 | (btr1<<8), CCBT);
675
676    if (c_can_disable_configuration(pchip))
677      return -ENODEV;
678
679    DEBUGMSG("-> ok\n");
680    return 0;
681 }
682
683 ///////////////////////////////////////////////////////////////////////
684 /*
685  * Starts the Chip, by setting the CAN Enable Bit
686  */
687 int c_can_start_chip(struct canchip_t *pchip)
688 {
689    u16 flags = 0;
690
691    DEBUGMSG("(c%d)calling c_can_start_chip(...)\n", pchip->chip_idx);
692
693    // Validate pointer
694    if ( NULL == pchip )
695      {
696         DEBUGMSG("-> Error Chip not available.\n");
697         return -1;
698      }
699
700 #ifdef C_CAN_WITH_CCCE
701    flags = c_can_read_reg_w(pchip, CCCE) | CE_EN;
702    c_can_write_reg_w(pchip, flags, CCCE);
703 #endif
704
705    DEBUGMSG("-> ok\n");
706 #ifdef REGDUMP
707    c_can_registerdump(pchip);
708 #endif
709
710    return 0;
711 }
712
713 ///////////////////////////////////////////////////////////////////////
714 /*
715  * Stops the Chip, by deleting the CAN Enable Bit
716  */
717 int c_can_stop_chip(struct canchip_t *pchip)
718 {
719    u16 flags = 0;
720
721    DEBUGMSG("(c%d)calling c_can_stop_chip(...)\n", pchip->chip_idx);
722
723    // Validate pointer
724    if ( NULL == pchip )
725      {
726         DEBUGMSG("-> Error Chip not available.\n");
727         return -1;
728      }
729
730 #ifdef C_CAN_WITH_CCCE
731    flags = c_can_read_reg_w(pchip, CCCE) & ~CE_EN;
732    c_can_write_reg_w(pchip, flags, CCCE);
733 #endif
734
735    DEBUGMSG("-> ok\n");
736    return 0;
737 }
738
739 ///////////////////////////////////////////////////////////////////////
740 /*
741  *Check the TxOK bit of the Status Register and resets it afterwards.
742  */
743 int c_can_check_tx_stat(struct canchip_t *pchip)
744 {
745    unsigned long tempstat = 0;
746
747    DEBUGMSG("(c%d)calling c_can_check_tx_stat(...)\n", pchip->chip_idx);
748
749    // Validate pointer
750    if ( NULL == pchip ) return -1;
751
752    tempstat = c_can_read_reg_w(pchip, CCSR);
753
754    if (tempstat & SR_TXOK)
755      {
756         c_can_write_reg_w(pchip, tempstat & ~SR_TXOK, CCSR);
757         return 0;
758      }
759    else
760      {
761         return 1;
762      }
763 }
764
765
766 ///////////////////////////////////////////////////////////////////////
767 int c_can_wakeup_tx(struct canchip_t *chip, struct msgobj_t *obj)
768 {
769         can_preempt_disable();
770         
771         can_msgobj_set_fl(obj,TX_REQUEST);
772
773         /* calls i82527_irq_write_handler synchronized with other invocations
774           from kernel and IRQ context */
775         c_can_irq_sync_activities(chip, obj);
776
777         can_preempt_enable();
778         return 0;
779 }
780
781 ///////////////////////////////////////////////////////////////////////
782 int c_can_filtch_rq(struct canchip_t *chip, struct msgobj_t *obj)
783 {
784         can_preempt_disable();
785         
786         can_msgobj_set_fl(obj,FILTCH_REQUEST);
787
788         /* setups filter synchronized with other invocations from kernel and IRQ context */
789         c_can_irq_sync_activities(chip, obj);
790
791         can_preempt_enable();
792         return 0;
793 }
794
795
796 ///////////////////////////////////////////////////////////////////////
797 void c_can_registerdump(struct canchip_t *pchip)
798 {
799    CANMSG("------------------------------------\n");
800    CANMSG("---------C-CAN Register Dump--------\n");
801    CANMSG("------------at 0x%.8lx-----------\n"
802           ,(unsigned long)pchip->chip_base_addr);
803    CANMSG("Control Register:             0x%.4lx\n",
804           (long)(c_can_read_reg_w( pchip, CCCR)));
805    CANMSG("Status Register:              0x%.4lx\n",
806           (long)(c_can_read_reg_w( pchip, CCSR)));
807    CANMSG("Error Counting Register:      0x%.4lx\n",
808           (long)(c_can_read_reg_w( pchip, CCEC)));
809    CANMSG("Bit Timing Register:          0x%.4lx\n",
810           (long)(c_can_read_reg_w( pchip, CCBT)));
811    CANMSG("Interrupt Register:           0x%.4lx\n",
812           (long)(c_can_read_reg_w( pchip, CCINTR)));
813    CANMSG("Test Register:                0x%.4lx\n",
814           (long)(c_can_read_reg_w( pchip, CCTR)));
815    CANMSG("Baud Rate Presc. Register:    0x%.4lx\n",
816           (long)(c_can_read_reg_w( pchip, CCBRPE)));
817 #ifdef C_CAN_WITH_CCCE
818    CANMSG("CAN Enable Register:          0x%.4lx\n",
819           (long)(c_can_read_reg_w( pchip, CCCE)));
820 #endif
821    CANMSG("Transm. Req. 1 Register:      0x%.4lx\n",
822           (long)(c_can_read_reg_w( pchip, CCTREQ1)));
823    CANMSG("Transm. Req. 2 Register:      0x%.4lx\n",
824           (long)(c_can_read_reg_w( pchip, CCTREQ2)));
825    CANMSG("New Data 1 Register:          0x%.4lx\n",
826           (long)(c_can_read_reg_w( pchip, CCND1)));
827    CANMSG("New Data 2 Register:          0x%.4lx\n",
828           (long)(c_can_read_reg_w( pchip, CCND2)));
829    CANMSG("Interrupt Pend. 1 Register:   0x%.4lx\n",
830           (long)(c_can_read_reg_w( pchip, CCINTP1)));
831    CANMSG("Interrupt Pend. 2 Register:   0x%.4lx\n",
832           (long)(c_can_read_reg_w( pchip, CCINTP2)));
833    CANMSG("------------------------------------\n");
834    CANMSG("IF1 Command Req. Register:    0x%.4lx\n",
835           (long)(c_can_read_reg_w( pchip, CCIF1CR)));
836    CANMSG("IF1 Command Mask Register:    0x%.4lx\n",
837           (long)(c_can_read_reg_w( pchip, CCIF1CM)));
838    CANMSG("IF1 Mask 1 Register:          0x%.4lx\n",
839           (long)(c_can_read_reg_w( pchip, CCIF1M1)));
840    CANMSG("IF1 Mask 2 Register:          0x%.4lx\n",
841           (long)(c_can_read_reg_w( pchip, CCIF1M2)));
842    CANMSG("IF1 Arbitration 1 Register:   0x%.4lx\n",
843           (long)(c_can_read_reg_w( pchip, CCIF1A1)));
844    CANMSG("IF1 Arbitration 2 Register:   0x%.4lx\n",
845           (long)(c_can_read_reg_w( pchip, CCIF1A2)));
846    CANMSG("IF1 Message Control Register: 0x%.4lx\n",
847           (long)(c_can_read_reg_w( pchip, CCIF1DMC)));
848    CANMSG("IF1 Data A1 Register:         0x%.4lx\n",
849           (long)(c_can_read_reg_w( pchip, CCIF1DA1)));
850    CANMSG("IF1 Data A2 Register:         0x%.4lx\n",
851           (long)(c_can_read_reg_w( pchip, CCIF1DA2)));
852    CANMSG("IF1 Data B1 Register:         0x%.4lx\n",
853           (long)(c_can_read_reg_w( pchip, CCIF1DB1)));
854    CANMSG("IF1 Data B2 Register:         0x%.4lx\n",
855           (long)(c_can_read_reg_w( pchip, CCIF1DB2)));
856    CANMSG("------------------------------------\n");
857    CANMSG("IF2 Command Req. Register:    0x%.4lx\n",
858           (long)(c_can_read_reg_w( pchip, CCIF2CR)));
859    CANMSG("IF2 Command Mask Register:    0x%.4lx\n",
860           (long)(c_can_read_reg_w( pchip, CCIF2CM)));
861    CANMSG("IF2 Mask 1 Register:          0x%.4lx\n",
862           (long)(c_can_read_reg_w( pchip, CCIF2M1)));
863    CANMSG("IF2 Mask 2 Register:          0x%.4lx\n",
864           (long)(c_can_read_reg_w( pchip, CCIF2M2)));
865    CANMSG("IF2 Arbitration 1 Register:   0x%.4lx\n",
866           (long)(c_can_read_reg_w( pchip, CCIF2A1)));
867    CANMSG("IF2 Arbitration 2 Register:   0x%.4lx\n",
868           (long)(c_can_read_reg_w( pchip, CCIF2A2)));
869    CANMSG("IF2 Message Control Register: 0x%.4lx\n",
870           (long)(c_can_read_reg_w( pchip, CCIF2DMC)));
871    CANMSG("IF2 Data A1 Register:         0x%.4lx\n",
872           (long)(c_can_read_reg_w( pchip, CCIF2DA1)));
873    CANMSG("IF2 Data A2 Register:         0x%.4lx\n",
874           (long)(c_can_read_reg_w( pchip, CCIF2DA2)));
875    CANMSG("IF2 Data B1 Register:         0x%.4lx\n",
876           (long)(c_can_read_reg_w( pchip, CCIF2DB1)));
877    CANMSG("IF2 Data B2 Register:         0x%.4lx\n",
878           (long)(c_can_read_reg_w( pchip, CCIF2DB2)));
879    CANMSG("------------------------------------\n");
880    CANMSG("------------------------------------\n");
881 }
882
883
884 void c_can_if1_registerdump(struct canchip_t *pchip)
885 {
886    CANMSG("----------------------------------------\n");
887    CANMSG("Error Counting Register:      0x%.4lx\n",
888           (long)(c_can_read_reg_w( pchip, CCEC)));
889    CANMSG("---------C-CAN IF1 Register Dump--------\n");
890    CANMSG("IF1 Command Req. Register:    0x%.4lx\n",
891           (long)(c_can_read_reg_w( pchip, CCIF1CR)));
892    CANMSG("IF1 Command Mask Register:    0x%.4lx\n",
893           (long)(c_can_read_reg_w( pchip, CCIF1CM)));
894    CANMSG("IF1 Mask 1 Register:          0x%.4lx\n",
895           (long)(c_can_read_reg_w( pchip, CCIF1M1)));
896    CANMSG("IF1 Mask 2 Register:          0x%.4lx\n",
897           (long)(c_can_read_reg_w( pchip, CCIF1M2)));
898    CANMSG("IF1 Arbitration 1 Register:   0x%.4lx\n",
899           (long)(c_can_read_reg_w( pchip, CCIF1A1)));
900    CANMSG("IF1 Arbitration 2 Register:   0x%.4lx\n",
901           (long)(c_can_read_reg_w( pchip, CCIF1A2)));
902    CANMSG("IF1 Message Control Register: 0x%.4lx\n",
903           (long)(c_can_read_reg_w( pchip, CCIF1DMC)));
904    CANMSG("IF1 Data A1 Register:         0x%.4lx\n",
905           (long)(c_can_read_reg_w( pchip, CCIF1DA1)));
906    CANMSG("IF1 Data A2 Register:         0x%.4lx\n",
907           (long)(c_can_read_reg_w( pchip, CCIF1DA2)));
908    CANMSG("IF1 Data B1 Register:         0x%.4lx\n",
909           (long)(c_can_read_reg_w( pchip, CCIF1DB1)));
910    CANMSG("IF1 Data B2 Register:         0x%.4lx\n",
911           (long)(c_can_read_reg_w( pchip, CCIF1DB2)));
912 }
913
914 ///////////////////////////////////////////////////////////////////////
915
916 int c_can_register(struct chipspecops_t *chipspecops)
917 {
918         CANMSG("initializing c_can chip operations\n");
919         chipspecops->chip_config=c_can_chip_config;
920         chipspecops->baud_rate=c_can_baud_rate;
921         /*chipspecops->standard_mask=c_can_standard_mask;
922         chipspecops->extended_mask=c_can_extended_mask;
923         chipspecops->message15_mask=c_can_extended_mask;*/
924         chipspecops->clear_objects=c_can_clear_objects;
925         /*chipspecops->config_irqs=c_can_config_irqs;*/
926         chipspecops->pre_read_config=c_can_pre_read_config;
927         chipspecops->pre_write_config=c_can_pre_write_config;
928         chipspecops->send_msg=c_can_send_msg;
929         chipspecops->check_tx_stat=c_can_check_tx_stat;
930         chipspecops->wakeup_tx=c_can_wakeup_tx;
931         chipspecops->filtch_rq = c_can_filtch_rq;
932         chipspecops->remote_request=c_can_remote_request;
933         chipspecops->enable_configuration=c_can_enable_configuration;
934         chipspecops->disable_configuration=c_can_disable_configuration;
935         chipspecops->set_btregs=c_can_set_btregs;
936         chipspecops->start_chip=c_can_start_chip;
937         chipspecops->stop_chip=c_can_stop_chip;
938         chipspecops->irq_handler=c_can_irq_handler;
939         chipspecops->irq_accept = NULL;
940         return 0;
941 }
942
943 int c_can_fill_chipspecops(struct canchip_t *chip)
944 {
945         chip->chip_type="c_can";
946         if(MAX_MSGOBJS >= 32) {
947                 chip->max_objects = 32;
948         } else {
949                 CANMSG("C_CAN requires 32 message objects per chip,"
950                        " but only %d is compiled maximum\n",MAX_MSGOBJS);
951                 chip->max_objects = MAX_MSGOBJS;
952         }
953         c_can_register(chip->chipspecops);
954         return 0;
955 }