]> rtime.felk.cvut.cz Git - lincan.git/blob - lincan/src/nsi_canpci.c
The LinCAN driver license unified according to DCE FEE CTU head and superiors request.
[lincan.git] / lincan / src / nsi_canpci.c
1 /**************************************************************************/
2 /* File: nsi_canpci.c - NSI CAN PCI card (2xi82527)                       */
3 /*                                                                        */
4 /* LinCAN - (Not only) Linux CAN bus driver                               */
5 /* Copyright (C) 2002-2009 DCE FEE CTU Prague <http://dce.felk.cvut.cz>   */
6 /* Copyright (C) 2002-2009 Pavel Pisa <pisa@cmp.felk.cvut.cz>             */
7 /* Copyright (C) 2004 Eric Pennamen <pennamen@gmail.com>                  */
8 /* Funded by OCERA and FRESCOR IST projects                               */
9 /* Based on CAN driver code by Arnaud Westenberg <arnaud@wanadoo.nl>      */
10 /* and Ake Hedman, eurosource <akhe@eurosource.se>                        */
11 /*                                                                        */
12 /* LinCAN is free software; you can redistribute it and/or modify it      */
13 /* under terms of the GNU General Public License as published by the      */
14 /* Free Software Foundation; either version 2, or (at your option) any    */
15 /* later version.  LinCAN is distributed in the hope that it will be      */
16 /* useful, but WITHOUT ANY WARRANTY; without even the implied warranty    */
17 /* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU    */
18 /* General Public License for more details. You should have received a    */
19 /* copy of the GNU General Public License along with LinCAN; see file     */
20 /* COPYING. If not, write to the Free Software Foundation, 675 Mass Ave,  */
21 /* Cambridge, MA 02139, USA.                                              */
22 /*                                                                        */
23 /* To allow use of LinCAN in the compact embedded systems firmware        */
24 /* and RT-executives (RTEMS for example), main authors agree with next    */
25 /* special exception:                                                     */
26 /*                                                                        */
27 /* Including LinCAN header files in a file, instantiating LinCAN generics */
28 /* or templates, or linking other files with LinCAN objects to produce    */
29 /* an application image/executable, does not by itself cause the          */
30 /* resulting application image/executable to be covered by                */
31 /* the GNU General Public License.                                        */
32 /* This exception does not however invalidate any other reasons           */
33 /* why the executable file might be covered by the GNU Public License.    */
34 /* Publication of enhanced or derived LinCAN files is required although.  */
35 /**************************************************************************/
36
37 #include "../include/can.h"
38 #include "../include/can_sysdep.h"
39 #include "../include/main.h"
40 #include "../include/nsi_canpci.h"
41 #include "../include/i82527.h"
42
43 extern int stdmask;
44 extern int extmask;
45 extern int mo15mask;
46
47 #define __NO_VERSION__
48 #include <linux/module.h>
49
50 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,10))
51         #define ioread32        can_readl
52         #define iowrite32       can_writel
53         #define ioread8         can_readb
54         #define iowrite8        can_writeb
55 #else
56 #endif
57
58
59
60 #define INT_CONF 0x00000040     /* value for register INTCSR of PLX */
61 #define NSI_VENDOR_ID           0x1637
62 #define NSI_CANPCI_DEVICE_ID    0x0001
63
64 enum PORT2 { P2_0=1, P2_1=1<<1, P2_2=1<<2, P2_3=1<<3, P2_4=1<<4, P2_5=1<<5, P2_6=1<<6, P2_7=1<<7 };
65
66 /*PLX register definition */
67 #define PLX_CNTRL       0x50    /* Controle register */
68 #define PLX_INTCSR      0x4C    /* Interruption controle register */
69
70 /* This value define the i82527 clock frequency */
71 #define iCLOCK          16000000
72
73
74 /* Il faut reserver 4 zones:
75  *              BAR0: 128 octets memoire (32bits) pour les registres du PLX9052
76  *              BAR1: 128 octets I/O pour les registres du PLX9052
77  *              BAR2: 256 octets memoire(8bits) pour les registres du PLX9052
78  *              BAR3: 256 octets memoire(8bits) pour les registres du PLX9052
79  */
80 /* Variables globales contenant les @ des IO-Memory apres remap */
81 #define NB_VALID_BAR    4
82
83 typedef struct {
84         void* addr_BAR_remap[NB_VALID_BAR];
85 }t_CardArray;
86
87 void nsi_canpci_connect_irq(struct candevice_t *candev)
88 {
89 //Not used
90 }
91 void nsi_canpci_disconnect_irq(struct candevice_t *candev)
92 {
93 //on disconnecting interrupt we need to disable interruption form PLX
94         iowrite32(0x0,(void*)(candev->io_addr+PLX_INTCSR));
95         DEBUGMSG("PLX interrupt disabled\n");
96 }
97
98 int nsi_canpci_config_irqs(struct canchip_t *chip, short irqs)
99 {
100         
101         unsigned long it_mask,it_reg;
102         struct candevice_t *candev;
103         it_mask=0;
104         DEBUGMSG("Configuring NSI CANPCI interrupt\n");
105         can_write_reg(chip,irqs,iCTL);
106         if( (irqs&0x0E)!=0)
107         {//At least one interrupt source requested
108                 if(chip->chip_idx==0)
109                 {
110                         DEBUGMSG("starting interrupt on chip 0\n");
111                         it_mask=1;
112                 }
113                 else
114                 {
115                         DEBUGMSG("starting interrupt on chip 1\n");
116                         it_mask=8;
117                 }
118                 candev=chip->hostdevice;
119                 it_reg = ioread32( (void*)(candev->io_addr+PLX_INTCSR));
120                 it_reg|=it_mask|0x40;
121                 iowrite32(it_reg,(void*)(candev->io_addr+PLX_INTCSR));
122         }
123         else
124         {//No more interrupt source
125                 if(chip->chip_idx==0)
126                 {
127                         DEBUGMSG("stoping interrupt on chip 0\n");
128                         it_mask=1;
129                 }
130                 else
131                 {
132                         DEBUGMSG("stoping interrupt on chip 1\n");
133                         it_mask=8;
134                 }
135                 candev=chip->hostdevice;
136                 it_reg = ioread32( (void*)(candev->io_addr+PLX_INTCSR));
137                 it_reg&=~it_mask;
138                 iowrite32(it_reg,(void*)(candev->io_addr+PLX_INTCSR));
139         }
140         return 0;
141 }
142
143 int nsi_canpci_i82527_chip_config(struct canchip_t *chip)
144 {
145         can_write_reg(chip,chip->int_cpu_reg,iCPU); // Configure cpu interface
146         can_write_reg(chip,(iCTL_CCE|iCTL_INI),iCTL); // Enable configuration
147         i82527_seg_write_reg(chip,chip->int_clk_reg,iCLK); // Set clock out slew rates 
148         i82527_seg_write_reg(chip,chip->int_bus_reg,iBUS); /* Bus configuration */
149         
150         can_write_reg(chip,P2_2|P2_1,iP2C); // The pin P2_2,P2_1 of the 527 must be set as output
151         can_write_reg(chip,P2_2|P2_1,iP2O); // and P2_2 must be set to 1
152         
153         can_write_reg(chip,0x00,iSTAT); /* Clear error status register */
154
155         /* Check if we can at least read back some arbitrary data from the 
156          * card. If we can not, the card is not properly configured!
157          */
158         canobj_write_reg(chip,chip->msgobj[1],0x25,iMSGDAT1);
159         canobj_write_reg(chip,chip->msgobj[2],0x52,iMSGDAT3);
160         canobj_write_reg(chip,chip->msgobj[10],0xc3,iMSGDAT6);
161         if ( (canobj_read_reg(chip,chip->msgobj[1],iMSGDAT1) != 0x25) ||
162               (canobj_read_reg(chip,chip->msgobj[2],iMSGDAT3) != 0x52) ||
163               (canobj_read_reg(chip,chip->msgobj[10],iMSGDAT6) != 0xc3) ) {
164                 CANMSG("Could not read back from the hardware.\n");
165                 CANMSG("This probably means that your hardware is not correctly configured!\n");
166                 return -1;
167         }
168         else
169                 DEBUGMSG("Could read back, hardware is probably configured correctly\n");
170
171         if (chip->baudrate == 0)
172                 chip->baudrate=1600000;
173
174         if (i82527_baud_rate(chip,chip->baudrate,chip->clock,0,75,0)) {
175                 CANMSG("Error configuring baud rate\n");
176                 return -ENODEV;
177         }
178         if (i82527_standard_mask(chip,0x0000,stdmask)) {
179                 CANMSG("Error configuring standard mask\n");
180                 return -ENODEV;
181         }
182         if (i82527_extended_mask(chip,0x00000000,extmask)) {
183                 CANMSG("Error configuring extended mask\n");
184                 return -ENODEV;
185         }
186         if (i82527_message15_mask(chip,0x00000000,mo15mask)) {
187                 CANMSG("Error configuring message 15 mask\n");
188                 return -ENODEV;
189         }
190         if (i82527_clear_objects(chip)) {
191                 CANMSG("Error clearing message objects\n");
192                 return -ENODEV;
193         }
194         
195         if (nsi_canpci_config_irqs(chip,iCTL_IE|iCTL_EIE)) { /* has been 0x0a */
196                 CANMSG("Error configuring interrupts\n");
197                 return -ENODEV;
198         }       
199         return 0;
200 }
201
202
203 int nsi_canpci_start_chip(struct canchip_t *chip)
204 {
205         unsigned long it_mask,it_reg;
206         struct candevice_t *candev;
207         it_mask=0;
208         if(chip->chip_idx==0)
209         {
210                 DEBUGMSG("starting chip 0\n");
211                 it_mask=1;
212         }
213         else
214         {
215                 DEBUGMSG("starting chip 1\n");
216                 it_mask=8;
217         }
218         candev=chip->hostdevice;
219         it_reg = ioread32( (void*)(candev->io_addr+PLX_INTCSR));
220         rmb();
221         it_reg|=it_mask|0x40;
222         iowrite32(it_reg,(void*)(candev->io_addr+PLX_INTCSR));
223         wmb();  
224         i82527_start_chip(chip);
225         return 0;
226 }
227
228 int nsi_canpci_stop_chip(struct canchip_t *chip)
229 {
230         unsigned long it_mask,it_reg;
231         struct candevice_t *candev;
232         it_mask=0;
233         if(chip->chip_idx==0)
234         {
235                 DEBUGMSG("stoping chip 0\n");
236                 it_mask=1;
237         }
238         else
239         {
240                 DEBUGMSG("stoping chip 1\n");
241                 it_mask=8;
242         }
243         candev=chip->hostdevice;
244         it_reg = ioread32( (void*)(candev->io_addr+PLX_INTCSR));
245         rmb();
246         it_reg&=~it_mask;
247         iowrite32(it_reg,(void*)(candev->io_addr+PLX_INTCSR));
248         wmb();  
249         i82527_stop_chip(chip);
250         return 0;
251 }
252
253 int nsi_canpci_irq_handler(int irq, struct canchip_t *chip)
254 {
255         int retcode;
256         unsigned long it_reg;
257         struct candevice_t *candev;
258         candev=chip->hostdevice;
259         retcode = CANCHIP_IRQ_NONE;
260         it_reg = ioread32( (void*)(candev->io_addr+PLX_INTCSR));
261         rmb();
262         if(chip->chip_idx==0)
263         {
264                 if((it_reg &0x4)!=0) //interrupt active
265                 {
266                         if(i82527_irq_handler(irq,chip)==CANCHIP_IRQ_NONE)
267                         {//some trouble with IT
268                                 it_reg&=~(0x01);
269                                 CANMSG("Unexcepted interruption from canal0, interruption is canceled\n");
270
271                         }else
272                         {
273                                 retcode=CANCHIP_IRQ_HANDLED;
274                         }
275                 
276                 }
277         }
278         else
279         {
280                 if((it_reg &0x20)!=0) //interrupt is set
281                 {
282                         if(i82527_irq_handler(irq,chip)==CANCHIP_IRQ_NONE)
283                         {//soucis avec les IT
284                                 it_reg&=~(0x08);
285                                 CANMSG("Unexcepted interruption from canal1, interruption is canceled\n");
286                         }else
287                         {
288                                 retcode=CANCHIP_IRQ_HANDLED;
289                         }       
290                 }
291         }
292         return retcode;
293 }
294
295 /* The function template_request_io is used to reserve the io-memory. If your
296  * hardware uses a dedicated memory range as hardware control registers you
297  * will have to add the code to reserve this memory as well.
298  * The reserved memory starts at candev->io_addr, wich is the module parameter io.
299  */
300 int nsi_canpci_request_io(struct candevice_t *candev)
301 {
302   (void)candev;
303   if(candev->dev_base_addr==0)
304         return -EIO;  
305   return 0;
306 }
307
308 /* The function template_release_io is used to free the previously reserved 
309  * io-memory. In case you reserved more memory, don't forget to free it here.
310  */
311 int nsi_canpci_release_io(struct candevice_t *candev)
312 {
313         unsigned long reg_reset;
314         struct pci_dev *pcidev = candev->sysdevptr.pcidev;
315         DEBUGMSG("Releasing board io\n");
316         
317         nsi_canpci_disconnect_irq(candev);
318         // First, set RESET signal to 0
319         reg_reset = ioread32( (void*)(candev->io_addr+PLX_CNTRL));
320         reg_reset&=(~(0x40000000));
321         rmb();
322         //Then set it to '1' for reseting the board
323         iowrite32( (reg_reset | 0x40000000 ),(void*)(candev->io_addr+PLX_CNTRL));
324         wmb();
325         udelay(2500); /* This delay must be greater than 1ms for i82527 */
326         iowrite32( (reg_reset ),(void*)(candev->io_addr+PLX_CNTRL)); //Releasing RESET signal
327         wmb();
328         udelay(2500); // Waiting for some additionnal time before writing in the 82527
329         iounmap(((t_CardArray*)(candev->dev_base_addr))->addr_BAR_remap[0]);
330         iounmap(((t_CardArray*)(candev->dev_base_addr))->addr_BAR_remap[1]);
331         iounmap(((t_CardArray*)(candev->dev_base_addr))->addr_BAR_remap[2]);
332         iounmap(((t_CardArray*)(candev->dev_base_addr))->addr_BAR_remap[3]);
333         kfree((void*)(candev->dev_base_addr));
334         pci_release_region(pcidev,0);
335         pci_release_region(pcidev,1);
336         pci_release_region(pcidev,2);                   
337         pci_release_region(pcidev,3);                   
338         return 0;
339 }
340
341 /* The function template_reset is used to give a hardware reset. This is rather
342  * hardware specific so I haven't included example code. Don't forget to check
343  * the reset status of the chip before returning.
344  */
345 int nsi_canpci_reset(struct candevice_t *candev)
346 {
347         unsigned long reg_reset;
348         
349         DEBUGMSG("Board reset !!!\n");
350         // Before reset disconnet interrupt to avoir freeze     
351         nsi_canpci_disconnect_irq(candev);
352         // First, set RESET signal to 0
353         reg_reset = ioread32( (void*)(candev->io_addr+PLX_CNTRL));
354         reg_reset&=(~(0x40000000));
355         //Then set it to '1' for reseting the board
356         iowrite32( (reg_reset | 0x40000000 ),(void*)(candev->io_addr+PLX_CNTRL));
357         wmb();
358         udelay(2500); /* This delay must be greater than 1ms for i82527 */
359         iowrite32(reg_reset,(void*)(candev->io_addr+PLX_CNTRL)); //Releasing RESET signal
360         wmb();
361         udelay(2500); // Waiting for some additionnal time before writing in the 82527
362         DEBUGMSG("Reset done !!!\n");
363         
364         nsi_canpci_connect_irq(candev);
365         return 0;       
366 }
367
368 /* The function template_init_hw_data is used to initialize the hardware
369  * structure containing information about the installed CAN-board.
370  * RESET_ADDR represents the io-address of the hardware reset register.
371  * NR_82527 represents the number of intel 82527 chips on the board.
372  * NR_SJA1000 represents the number of philips sja1000 chips on the board.
373  * The flags entry can currently only be CANDEV_PROGRAMMABLE_IRQ to indicate that
374  * the hardware uses programmable interrupts.
375  */
376
377 int nsi_canpci_init_hw_data(struct candevice_t *candev) 
378      {
379   struct pci_dev *pcidev = NULL;
380
381   /* looking for NSI CANPCI ident on the pci bus*/
382   do
383   {
384     pcidev = pci_find_device(NSI_VENDOR_ID, NSI_CANPCI_DEVICE_ID, pcidev);
385   }
386   while(can_check_dev_taken(pcidev));
387   
388   if(pcidev == NULL) 
389   {
390         do
391         {
392         pcidev = pci_find_device(NSI_VENDOR_ID, NSI_CANPCI_DEVICE_ID+1, pcidev);
393         }
394         while(can_check_dev_taken(pcidev));
395         if(pcidev == NULL) 
396         {
397                 CANMSG ("Error : NSI CAN PCI device not found\n");
398                 return -ENODEV;
399         }
400         else
401         {
402                 CANMSG ("NSI CANPCI OPTO device found\n");
403         }
404   }
405   else
406   {
407         CANMSG ("NSI CANPCI device found\n");  
408   }
409     
410   /* enable it */
411   if (pci_enable_device (pcidev))
412   {
413     CANMSG ("Cannot enable PCI device\n");
414     return -EIO;
415   }
416   CANMSG ("NSI CANPCI device started\n");
417   candev->sysdevptr.pcidev = pcidev;
418   candev->res_addr=0;
419   candev->nr_82527_chips=2;
420   candev->nr_sja1000_chips=0;
421   candev->nr_all_chips=2; 
422   /* initialize device spinlock */
423   can_spin_lock_init(&candev->device_lock);
424
425   if(pci_request_region(pcidev,0,"nsi_canpci bar0")==0)
426   {
427         if(pci_request_region(pcidev,1,"nsi_canpci bar1")==0)
428         {
429                 if(pci_request_region(pcidev,2,"nsi_canpci bar2")==0)
430                 {
431                         if(pci_request_region(pcidev,3,"nsi_canpci bar3")==0)
432                         {
433                         }
434                         else
435                         {
436                         pci_release_region(pcidev,0);
437                         pci_release_region(pcidev,1);
438                         pci_release_region(pcidev,2);                   
439                         return -EIO;
440                         }
441                 }
442                 else
443                 {
444                 pci_release_region(pcidev,0);
445                 pci_release_region(pcidev,1);
446                 return -EIO;
447                 }
448         }
449         else
450         {
451         pci_release_region(pcidev,0);
452         return -EIO;
453         }
454   }  
455   else
456   {
457         return -EIO;
458   }
459   candev->dev_base_addr=(unsigned long)(kmalloc(sizeof(t_CardArray),GFP_ATOMIC));  
460   
461   if((unsigned long)candev->dev_base_addr==0)
462         return -EIO;
463   //PLX register 
464   ((t_CardArray*)(candev->dev_base_addr))->addr_BAR_remap[0]=ioremap(pci_resource_start(pcidev,0),pci_resource_len(pcidev,0) );
465   //PLX IO
466   ((t_CardArray*)(candev->dev_base_addr))->addr_BAR_remap[1]=ioremap(pci_resource_start(pcidev,1),pci_resource_len(pcidev,1) );
467   //Chip 0
468   ((t_CardArray*)(candev->dev_base_addr))->addr_BAR_remap[2]=ioremap(pci_resource_start(pcidev,2),pci_resource_len(pcidev,2) );
469   //Chip 1
470   ((t_CardArray*)(candev->dev_base_addr))->addr_BAR_remap[3]=ioremap(pci_resource_start(pcidev,3),pci_resource_len(pcidev,3) );
471   
472   //Short acces to plx register
473   candev->io_addr=(unsigned long)(((t_CardArray*)(candev->dev_base_addr))->addr_BAR_remap[0]);
474   return 0;  
475 }
476
477 /* The function template_init_chip_data is used to initialize the hardware
478  * structure containing information about the CAN chips.
479  * CHIP_TYPE represents the type of CAN chip. CHIP_TYPE can be "i82527" or
480  * "sja1000".
481  * The chip_base_addr entry represents the start of the 'official' memory map
482  * of the installed chip. It's likely that this is the same as the candev->io_addr
483  * argument supplied at module loading time.
484  * The clock argument holds the chip clock value in Hz.
485  */
486
487 int nsi_canpci_init_chip_data(struct candevice_t *candev, int chipnr)
488 {
489         //u8 irq_line;
490         CANMSG ("NSI chip data init %d\n",chipnr);
491         i82527_fill_chipspecops(candev->chip[chipnr]);
492         
493         candev->chip[chipnr]->chipspecops->chip_config =nsi_canpci_i82527_chip_config;
494         candev->chip[chipnr]->chipspecops->start_chip=nsi_canpci_start_chip;
495         candev->chip[chipnr]->chipspecops->stop_chip=nsi_canpci_stop_chip;
496         candev->chip[chipnr]->chipspecops->config_irqs=nsi_canpci_config_irqs;
497         candev->chip[chipnr]->chipspecops->irq_handler=nsi_canpci_irq_handler;
498         /*candev->chip[chipnr]->chip_data = NULL;*/
499         
500         candev->chip[chipnr]->chip_base_addr= (unsigned long) (((t_CardArray*)(candev->dev_base_addr))->addr_BAR_remap[chipnr+2]);
501         candev->chip[chipnr]->clock = iCLOCK;
502         candev->chip[chipnr]->chip_irq=candev->sysdevptr.pcidev->irq;
503         candev->chip[chipnr]->flags=CHIP_IRQ_PCI;
504         candev->chip[chipnr]->int_cpu_reg = iCPU_DSC+iCPU_CEN;
505         candev->chip[chipnr]->int_clk_reg = iCLK_SL1+iCLK_CD0;
506         candev->chip[chipnr]->int_bus_reg = iBUS_CBY;
507         return 0;
508 }
509
510  /* The function template_init_obj_data is used to initialize the hardware
511  * structure containing information about the different message objects on the
512  * CAN chip. In case of the sja1000 there's only one message object but on the
513  * i82527 chip there are 15.
514  * The code below is for a i82527 chip and initializes the object base addresses
515  * The entry obj_base_addr represents the first memory address of the message 
516  * object. In case of the sja1000 obj_base_addr is taken the same as the chips
517  * base address.
518  * Unless the hardware uses a segmented memory map, flags can be set zero.
519  */
520 int nsi_canpci_init_obj_data(struct canchip_t *chip, int objnr)
521 {
522
523         
524         chip->msgobj[objnr]->obj_base_addr=
525             chip->chip_base_addr+(objnr+1)*0x10;
526         
527         return 0;
528 }
529
530 /* The function template_program_irq is used for hardware that uses programmable
531  * interrupts. If your hardware doesn't use programmable interrupts you should
532  * not set the candevices_t->flags entry to CANDEV_PROGRAMMABLE_IRQ and leave this
533  * function unedited. Again this function is hardware specific so there's no
534  * example code.
535  */
536 int nsi_canpci_program_irq(struct candevice_t *candev)
537 {
538         return 0;
539 }
540
541 /* The function template_write_register is used to write to hardware registers
542  * on the CAN chip. You should only have to edit this function if your hardware
543  * uses some specific write process.
544  */
545 void nsi_canpci_write_register(unsigned data, can_ioptr_t address)
546 {
547         iowrite8((u8)data,(void*)address);
548 }
549
550 /* The function template_read_register is used to read from hardware registers
551  * on the CAN chip. You should only have to edit this function if your hardware
552  * uses some specific read process.
553  */
554 unsigned nsi_canpci_read_register(can_ioptr_t address)
555 {
556         return ioread8((void*)address);
557 }
558
559
560  /* !!! Don't change this function !!! */
561 int nsi_canpci_register(struct hwspecops_t *hwspecops)
562 {
563         hwspecops->request_io = nsi_canpci_request_io;
564         hwspecops->release_io = nsi_canpci_release_io;
565         hwspecops->reset = nsi_canpci_reset;
566         hwspecops->init_hw_data = nsi_canpci_init_hw_data;
567         hwspecops->init_chip_data = nsi_canpci_init_chip_data;
568         hwspecops->init_obj_data = nsi_canpci_init_obj_data;
569         hwspecops->write_register = nsi_canpci_write_register;
570         hwspecops->read_register = nsi_canpci_read_register;
571         hwspecops->program_irq = nsi_canpci_program_irq;
572         return 0;
573 }