]> rtime.felk.cvut.cz Git - lincan.git/blob - lincan/src/ipci165.c
Committed IXXAT iPC-I 165 (PCI) implemented by Radim Kalas from Unicontrols.cz.
[lincan.git] / lincan / src / ipci165.c
1 /* ipci165.c
2  * Linux CAN-bus device driver for IXXAT iPC-I 165 (PCI) compatible HW.
3  * Written for new CAN driver version by Radim Kalas
4  * email:kalas@unicontrols.cz
5  * This software is released under the GPL-License.
6  * Version lincan-0.3  17 Jun 2004
7  */
8
9 #include "../include/can.h"
10 #include "../include/can_sysdep.h"
11 #include "../include/main.h"
12 #include "../include/setup.h"
13 #include "../include/finish.h"
14 #include "../include/ipci165.h"
15 #include "../include/ipci165_fw.h"
16 #include "../include/kthread.h"
17
18 #include <ctype.h>
19
20 can_irqreturn_t ipci165_irq_handler(int irq, void *dev_id, struct pt_regs *regs);
21 int ipci165_baud_rate(struct canchip_t *chip, int rate, int clock, int sjw,
22                       int sampl_pt, int flags);
23 int ipci165_set_btregs(struct canchip_t *chip, unsigned short btr0,
24                        unsigned short btr1);
25 int ipci165_start_chip(struct canchip_t *chip);
26
27 #ifdef CAN_DEBUG
28   void dump_mem(char *ptr, int size);
29 #else
30 #define dump_mem(a,b)
31 #endif
32
33 #define ipci165_load_btr(btr,btr0,btr1) {*btr = btr0; *(btr+1) = btr1;}
34
35 /**
36  * ipci165_delay - Delay the execution
37  * @msdelay: milliseconds to wait
38  *
39  * Return value: no return value
40  * File: src/ipci165.c
41  */
42 static void ipci165_delay(long msdelay)
43 {
44 #ifdef CAN_WITH_RTL
45   if(!rtl_rt_system_is_idle())
46   {
47     rtl_delay(1000000l*msdelay);
48   } else
49 #endif /*CAN_WITH_RTL*/
50   {
51     set_current_state(TASK_UNINTERRUPTIBLE);
52     schedule_timeout((msdelay*HZ)/1000+1);
53   }
54 }
55
56 /**
57  * ipci165_generate_irq - Generate irq for HW
58  * @candev: Pointer to hardware/board specific functions
59  *
60  * Return value: The function returns zero on success or non zero on failure
61  * File: src/ipci165.c
62  */
63 void ipci165_generate_irq(struct candevice_t *candev)
64 {
65   unsigned long crm_addr = candev->io_addr;
66   writeb(readb(crm_addr + CRM_UCR) & 0xFB, crm_addr + CRM_UCR);
67   writeb(readb(crm_addr + CRM_UCR) | 0x04, crm_addr + CRM_UCR);
68 }
69
70 /**
71  * bci_command - Send command to controller
72  * @candev: Pointer to hardware/board specific functions
73  * @cmd: Command to be performed
74  * @size: Command data size
75  * @data: Command data
76  *
77  * Return value: The function returns zero on success or non zero on failure
78  * File: src/ipci165.c
79  */
80 int bci_command(struct candevice_t *candev, char cmd, int size, char *data)
81 {
82   unsigned long dpram_addr = candev->dev_base_addr;
83
84   DEBUGMSG ("ipci165_bci_command\n");
85
86   if (size > BCI_CMD_MAX_LEN)
87   {
88     DEBUGMSG ("ipci165_bci_command: parameter error\n");
89     return -EINVAL;
90   }
91
92   /* grant access to the command buffer */
93   can_spin_lock(candev->device_lock);
94
95   // check command buffer status
96   if (readb(dpram_addr + OF_BCI_SYNC) != 0)
97   {
98     /* something went wrong ... */
99     can_spin_unlock(candev->device_lock);
100     DEBUGMSG ("ipci165_bci_command: command buffer is busy\n");
101     return (-EBUSY);
102   }
103
104   // prepare command
105   writeb(cmd, dpram_addr + OF_BCI_CMD);
106   writeb(size + 1, dpram_addr + OF_BCI_NUM);
107   memcpy_toio(dpram_addr + OF_BCI_DATA, data, size);
108
109   // set flag for firmware
110   writeb(1, dpram_addr + OF_BCI_SYNC);
111
112   // generate interrupt to microcontroller
113   ipci165_generate_irq (candev);
114
115   return 0;
116 }
117
118 /**
119  * bci_response - Get response from controller
120  * @candev: Pointer to hardware/board specific functions
121  * @cmd: Command to get response for
122  * @size: Command data size
123  * @data: Command data
124  *
125  * Return value: The function returns zero on success or non zero on failure
126  * File: src/ipci165.c
127  */
128 int bci_response(struct candevice_t *candev, char cmd, int *size, char *data)
129 {
130   unsigned long dpram_addr = candev->dev_base_addr;
131   char tmp;
132   int delay;
133
134   DEBUGMSG ("ipci165_bci_response\n");
135
136   delay = 1000;
137   while (readb(dpram_addr + OF_BCI_SYNC) != 2)
138   {
139     /* wait 1 ms */
140     /*    ipci165_delay(1); */
141     udelay(100);
142     if (--delay == 0)
143     {
144       /* timeout occured */
145       /* release the lock */
146       can_spin_unlock(candev->device_lock);
147       CANMSG ("BCI timeout!\n");
148       return -EBUSY;
149     }
150   }
151
152   /* we will not copy the command filed, so decrement the size by 1 */
153   tmp = readb(dpram_addr + OF_BCI_NUM) - 1;
154   if (*size > tmp) *size = tmp;
155
156   if (readb(dpram_addr + OF_BCI_CMD) != cmd)
157   {
158     /* release the buffer */
159     writeb(0, dpram_addr + OF_BCI_SYNC);
160     /* unlock the access */
161     can_spin_unlock(candev->device_lock);
162
163     DEBUGMSG ("ipci165_bci_command: invalid answer\n");
164     return -EIO;
165   }
166   memcpy_fromio(data, dpram_addr + OF_BCI_DATA, *size);
167
168   /* release the buffer */
169   writeb(0, dpram_addr + OF_BCI_SYNC);
170   /* unlock the access */
171   can_spin_unlock(candev->device_lock);
172   return 0;
173 }
174
175 /**
176  * ipci165_restart_can - Flush queues and sestart can controller
177  * @candev: Pointer to hardware/board specific functions
178  * @chip_idx: chip number
179  *
180  * Return value: The function returns zero on success or non zero on failure
181  * File: src/ipci165.c
182  */
183 int ipci165_restart_can(struct canchip_t *chip)
184 {
185   char data[3];
186   int size;
187   int i;
188
189   struct ipci165_chip_t *chip_data;
190   unsigned long msg_ofs;
191
192   /* reset CAN */
193   data[0] = chip->chip_idx;
194   size = 1;
195   if (bci_command(chip->hostdevice, CMD_RESET_CAN, 1, data) ||
196       bci_response(chip->hostdevice, CMD_RESET_CAN, &size, data) ||
197       (data[0] == 0))
198   {
199     CANMSG ("CAN reset failed!\n");
200     return -ENODEV;
201   }
202
203   /* flush TX/RX queues in DP-RAM */
204   chip_data = (struct ipci165_chip_t *)chip->chip_data;
205   msg_ofs = BCI_MSG_STATUS;
206
207   for (i = 0; i< BCI_QUEUE_SIZE; i++)
208   {
209     writeb(BCI_MSG_STATUS_FREE, chip_data->rx_queue.addr + msg_ofs);
210     writeb(BCI_MSG_STATUS_FREE, chip_data->tx_queue.addr + msg_ofs);
211     msg_ofs += BCI_MSG_SIZE;
212   }
213
214   /* In- and output buffer re-initialization */
215   canqueue_ends_flush_inlist(chip->msgobj[0]->qends);
216   canqueue_ends_flush_outlist(chip->msgobj[0]->qends);
217
218   /* start CAN */
219   data[0] = chip->chip_idx;
220   size = 1;
221   if (bci_command(chip->hostdevice, CMD_START_CAN, 1, data) ||
222       bci_response(chip->hostdevice, CMD_START_CAN, &size, data) ||
223       (data[0] == 0))
224   {
225     CANMSG ("start chip failed!\n");
226     return -ENODEV;
227   }
228   return 0;
229 }
230
231 /* this is the thread function that we are executing */
232 /**
233  * ipci165_kthread - Thread restarting can controller after bus-off.
234  * @kthread: pointer to kernel thread descriptor
235  * @chip_idx: chip number
236  *
237  * Return value: no return value
238  * File: src/ipci165.c
239  */
240 void ipci165_kthread(kthread_t *kthread)
241 {
242   struct canchip_t *chip = (struct canchip_t *)kthread->arg;
243   struct ipci165_chip_t *chip_data = (struct ipci165_chip_t *)chip->chip_data;
244
245   /* setup the thread environment */
246   init_kthread(kthread, "ipci165");
247
248   /* this is normal work to do */
249   CANMSG ("kernel thread started!\n");
250
251   /* an endless loop in which we are doing our work */
252   for(;;)
253   {
254     /* fall asleep */
255     wait_event_interruptible(kthread->queue,test_bit(CHIP_FLAG_BUS_OFF,&chip_data->flags));
256
257     /* We need to do a memory barrier here to be sure that
258     the flags are visible on all CPUs. */
259     mb();
260
261     /* here we are back from sleep because we caught a signal. */
262     if (kthread->terminate)
263     {
264       /* we received a request to terminate ourself */
265       break;
266     }
267
268     {
269       clear_bit(CHIP_FLAG_BUS_OFF,&chip_data->flags);
270       set_bit(CHIP_FLAG_RESET,&chip_data->flags);
271       /* this is normal work to do */
272       ipci165_restart_can(chip);
273
274       clear_bit(CHIP_FLAG_RESET,&chip_data->flags);
275
276       /* wait at least 100ms for next reset */
277       ipci165_delay(100);
278     }
279   }
280   /* here we go only in case of termination of the thread */
281
282   /* cleanup the thread, leave */
283   CANMSG ("kernel thread terminated!\n");
284   exit_kthread(kthread);
285
286   /* returning from the thread here calls the exit functions */
287 }
288
289 /**
290  * ipci165_qfull_latency - Compute delay to send out full tx queue
291  * @candev: Pointer to candevice/board structure
292  * @obj: pointer to message object state structure
293  *
294  * Return Value: The function returns computed delay in jiffies
295  * File: src/ipci165.c
296  */
297 long ipci165_qfull_latency(struct msgobj_t *obj)
298 {
299   long latency;
300   latency = obj->hostchip->baudrate;
301   if(latency){
302     latency=(long)HZ*(CAN_FRAME_MIN_BIT_LEN * BCI_QUEUE_SIZE)/latency + 1;
303   }
304
305   return latency;
306 }
307
308 /**
309  * ipci165_connect_irq: Installs interrupt routine and enable irq on HW
310  * @candev: Pointer to candevice/board structure
311  *
312  * Return Value: The function returns zero on success or %-ENODEV on failure
313  * File: src/ipci165.c
314  */
315 int ipci165_connect_irq(struct candevice_t *candev)
316 {
317   unsigned long crm_addr = candev->io_addr;
318   unsigned char icr;
319   DEBUGMSG ("ipci165_connect_irq\n");
320
321   /* install interrupt routine */
322   if (request_irq(candev->sysdevptr.pcidev->irq,
323                   ipci165_irq_handler,
324                   SA_SHIRQ,
325                   DEVICE_NAME,
326                   candev))
327     return -ENODEV;
328
329   // Enable interrupt to PC
330   writeb(readb(crm_addr + CRM_ICR) | 0x40, crm_addr + CRM_ICR);
331   udelay (100);
332   icr = readb(crm_addr + CRM_ICR);
333   return 0;
334 }
335
336 /**
337  * ipci165_disconnect_irq - Disable irq on HW
338  * @candev: Pointer to candevice/board structure
339  *
340  * Return Value: The function returns zero on success or %-ENODEV on failure
341  * File: src/ipci165.c
342  */
343 void ipci165_disconnect_irq(struct candevice_t *candev)
344 {
345   unsigned long crm_addr = candev->io_addr;
346   unsigned char icr;
347   DEBUGMSG ("ipci165_disconnect_irq\n");
348
349   // Enable interrupt to PC
350   writeb(readb(crm_addr + CRM_ICR) & ~0x40, crm_addr + CRM_ICR);
351   udelay (100);
352   icr = readb(crm_addr + CRM_ICR);
353   /* deinstall interrupt routine */
354   free_irq(candev->sysdevptr.pcidev->irq, candev);
355 }
356
357 /* * * CAN Functionality * * */
358
359 /**
360  * ipci165_chip_config - Can chip configuration
361  * @chip: pointer to chip state structure
362  *
363  * Return Value: negative value reports error.
364  * File: src/ipci165.c
365  */
366 int ipci165_chip_config(struct canchip_t *chip)
367 {
368   struct ipci165_chip_t *chip_data = chip->chip_data;
369   char data[3];
370   int ret, size;
371
372   DEBUGMSG ("ipci165_chip_config[%i]\n",chip->chip_idx);
373
374   /* comupte the base address of tx and rx queue for the channel */
375   chip_data->tx_queue.addr = chip->chip_base_addr + OF_CH1_TX_QUEUE +
376       chip->chip_idx * (OF_CH2_TX_QUEUE-OF_CH1_TX_QUEUE);
377   chip_data->rx_queue.addr = chip->chip_base_addr + OF_CH1_RX_QUEUE +
378       chip->chip_idx * (OF_CH2_RX_QUEUE-OF_CH1_RX_QUEUE);
379
380   /* reset CAN */
381   data[0] = chip->chip_idx;
382
383   size = 1;
384   if (bci_command(chip->hostdevice, CMD_RESET_CAN, 1, data) ||
385       bci_response(chip->hostdevice, CMD_RESET_CAN, &size, data) ||
386       (data[0] == 0))
387   {
388     CANMSG ("CAN reset failed!\n");
389     return -ENODEV;
390   }
391
392   /* configure rx queue */
393   data[0] = chip->chip_idx;
394   data[1] = BCI_LATENCY_MODE;
395   data[2] = 0; /* dummy */
396
397   size = 1;
398   if (bci_command(chip->hostdevice, CMD_CONFIG_RX_QUEUE, 3, data) ||
399       bci_response(chip->hostdevice, CMD_CONFIG_RX_QUEUE, &size, data) ||
400       (data[0] == 0))
401   {
402     CANMSG ("config RX queue failed!\n");
403     return -ENODEV;
404   }
405   /* setup baud rate */
406   if (!chip->baudrate) chip->baudrate = 1000000;
407   if ((ret = ipci165_baud_rate(chip, chip->baudrate, chip->clock, 0, 0, 0))) return ret;
408
409   /* start can communication */
410   if ((ret = ipci165_start_chip(chip))) return ret;
411
412   return 0;
413 }
414
415 /**
416  * ipci165_baud_rate - Set communication parameters
417  * @chip: pointer to chip state structure
418  * @rate: baud rate in Hz
419  * @clock: not used
420  * @sjw: not used
421  * @sampl_pt: not used
422  * @flags: not used
423  *
424  * Return Value: negative value reports error.
425  * File: src/ipci165.c
426  */
427 int ipci165_baud_rate(struct canchip_t *chip, int rate, int clock, int sjw,
428                       int sampl_pt, int flags)
429 {
430   DEBUGMSG ("ipci165_baud_rate[%i]\n",chip->chip_idx);
431
432   switch (rate) {
433     case 10000:  return ipci165_set_btregs(chip, BCI_10KB);
434     case 20000:  return ipci165_set_btregs(chip, BCI_20KB);
435     case 50000:  return ipci165_set_btregs(chip, BCI_50KB);
436     case 100000: return ipci165_set_btregs(chip, BCI_100KB);
437     case 125000: return ipci165_set_btregs(chip, BCI_125KB);
438     case 250000: return ipci165_set_btregs(chip, BCI_250KB);
439     case 500000: return ipci165_set_btregs(chip, BCI_500KB);
440     case 1000000:return ipci165_set_btregs(chip, BCI_1000KB);
441     default: return -EINVAL;
442   }
443
444   return 0;
445 }
446
447 /**
448  * ipci165_set_btregs - Configure bitrate registers
449  * @chip: pointer to chip state structure
450  * @btr0: bitrate register 0
451  * @btr1: bitrate register 1
452  *
453  * Return Value: negative value reports error.
454  * File: src/ipci165.c
455  */
456 int ipci165_set_btregs(struct canchip_t *chip, unsigned short btr0,
457                        unsigned short btr1)
458 {
459   unsigned char data[3];
460   int size;
461
462   DEBUGMSG ("ipci165_set_btregs[%i]: btr0=%02x, btr1=%02x\n",chip->chip_idx,
463             (unsigned)btr0,(unsigned)btr1);
464   
465   /* configure the chip */
466   data[0] = chip->chip_idx;
467   data[1] = btr0;
468   data[2] = btr1;
469   
470   size = 1;
471   if (bci_command(chip->hostdevice, CMD_INIT_CAN, 3, data) ||
472       bci_response(chip->hostdevice, CMD_INIT_CAN, &size, data) ||
473       (data[0] == 0))
474   {
475     CANMSG ("baud rate setup failed!\n");
476     return -ENODEV;
477   }
478   return 0;
479 }
480
481 /**
482  * ipci165_stop_chip - Start chip message processing
483  * @chip: pointer to chip state structure
484  *
485  * Return Value: negative value reports error.
486  * File: src/ipci165.c
487  */
488 int ipci165_start_chip(struct canchip_t *chip)
489 {
490   char data[1];
491   int size;
492
493   DEBUGMSG ("ipci165_start_chip[%i]\n",chip->chip_idx);
494   
495   /* start CAN */
496   data[0] = chip->chip_idx;
497   
498   size = 1;
499   if (bci_command(chip->hostdevice, CMD_START_CAN, 1, data) ||
500       bci_response(chip->hostdevice, CMD_START_CAN, &size, data) ||
501       (data[0] == 0))
502   {
503     CANMSG ("start chip failed!\n");
504     return -ENODEV;
505   }
506   return 0;
507 }
508
509 /**
510  * ipci165_stop_chip -  Stop chip message processing
511  * @chip: pointer to chip state structure
512  *
513  * Return Value: negative value reports error.
514  * File: src/ipci165.c
515  */
516 int ipci165_stop_chip(struct canchip_t *chip)
517 {
518   char data[1];
519   int size;
520
521   DEBUGMSG ("ipci165_stop_chip[%i]\n",chip->chip_idx);
522   
523   /* configure the chip */
524   data[0] = chip->chip_idx;
525   
526   size = 1;
527   if (bci_command(chip->hostdevice, CMD_STOP_CAN, 1, data) ||
528       bci_response(chip->hostdevice, CMD_STOP_CAN, &size, data) ||
529       (data[0] == 0))
530   {
531     CANMSG ("stop chip failed!\n");
532     return -ENODEV;
533   }
534   return 0;
535 }
536
537 /**
538  * ipci165_pre_read_config - Prepare message object for message reception
539  * @chip: pointer to chip state structure
540  * @obj: pointer to message object state structure
541  *
542  * Return Value: negative value reports error.
543  *      Positive value indicates immediate reception of message.
544  * File: src/ipci165.c
545  */
546 int ipci165_pre_read_config(struct canchip_t *chip, struct msgobj_t *obj)
547 {
548   return 0;
549 }
550
551 /**
552  * ipci165_pre_write_config - Prepare message object for message transmission
553  * @chip: pointer to chip state structure
554  * @obj: pointer to message object state structure
555  * @msg: pointer to CAN message
556  *
557  * Return Value: negative value reports error.
558  * File: src/ipci165.c
559  */
560 int ipci165_pre_write_config(struct canchip_t *chip, struct msgobj_t *obj,
561                              struct canmsg_t *msg)
562 {
563   return 0;
564 }
565
566 /**
567  * ipci165_send_msg - Initiate message transmission
568  * @chip: pointer to chip state structure
569  * @obj: pointer to message object state structure
570  * @msg: pointer to CAN message
571  *
572  * This function is called after ipci165_pre_write_config() function,
573  * which prepares data in chip buffer.
574  * Return Value: negative value reports error.
575  * File: src/ipci165.c
576  */
577 int ipci165_send_msg(struct canchip_t *chip, struct msgobj_t *obj,
578                      struct canmsg_t *msg)
579 {
580   return 0;
581 }
582
583 /**
584  * ipci165_check_tx_stat - Checks state of transmission engine
585  * @chip: pointer to chip state structure
586  *
587  * Return Value: negative value reports error.
588  *      Positive return value indicates transmission under way status.
589  *      Zero value indicates finishing of all issued transmission requests.
590  * File: src/ipci165.c
591  */
592 int ipci165_check_tx_stat(struct canchip_t *chip)
593 {
594   return 0;
595 }
596
597 /**
598  * ipci165_irq_read_handler - ISR code responsible for receiving
599  * @chip: pointer to chip state structure
600  * @obj: pointer to attached queue description
601  *
602  * The main purpose of this function is to read message from CAN controller and
603  * transfer them to attached queues
604  * File: src/ipci165.c
605  */
606 void ipci165_irq_read_handler(struct canchip_t *chip, struct msgobj_t *obj)
607 {
608   struct ipci165_chip_t *chip_data = (struct ipci165_chip_t *)chip->chip_data;
609   struct bci_queue_t *queue = &(chip_data)->rx_queue;
610   unsigned long       queue_addr = queue->addr;
611   unsigned long       msg_addr   = queue_addr + queue->idx * BCI_MSG_SIZE;
612
613   int len;
614   unsigned char frame_info;
615   unsigned status;
616   unsigned short tmp16;
617   unsigned long  tmp32;
618
619   DEBUGMSG ("ipci165_irq_read_handler[%i]\n",chip->chip_idx);
620
621   do {
622     dump_mem(msg_addr, BCI_MSG_SIZE);
623     if (readb(msg_addr + BCI_MSG_TYPE) == BCI_MSG_TYPE_CAN)
624     {
625 #if 0
626       printk("ST(0)=%x, ST(1)=%x\n",readw(chip->chip_base_addr+OF_CAN1_STATUS),
627              readw(chip->chip_base_addr+OF_CAN2_STATUS));
628       for (tmp16 = 0 ; tmp16 < BCI_QUEUE_SIZE ; tmp16 ++)
629         printk ("MSG_ST(%i)=%x\n",tmp16,readb(chip->chip_base_addr + OF_CH2_TX_QUEUE + tmp16*BCI_MSG_SIZE + BCI_MSG_STATUS));
630       /* this is a can message */
631       DEBUGMSG ("ipci165_irq_read_handler[%i]: message in buffer\n",chip->chip_idx);
632 #endif
633
634       frame_info = readb(msg_addr + BCI_MSG_FRAME);
635       len =  frame_info & 0x0f;
636       if(len > CAN_MSG_LENGTH) len = CAN_MSG_LENGTH;
637       obj->rx_msg.length = len;
638       obj->rx_msg.flags  = (frame_info & BCI_MSG_FRAME_RTR ? MSG_RTR : 0);
639       obj->rx_msg.cob    = 0;
640       obj->rx_msg.timestamp.tv_sec = 0;
641       obj->rx_msg.timestamp.tv_usec = 
642           BCI_TIMESTAMP_RES * readl(msg_addr + BCI_MSG_TIMESTAMP);
643       /*  BCI_TIMESTAMP_RES * le32_to_cpu(readl(msg_addr + BCI_MSG_TIMESTAMP)); */
644
645       /* fill CAN message timestamp */
646       /* can_filltimestamp(&obj->rx_msg.timestamp); */
647
648       if (frame_info & BCI_MSG_FRAME_EXT)
649       {
650         /* extended frame - 29 bit identifier */
651         obj->rx_msg.flags |= MSG_EXT;
652         /* the ID is stored in motorola format (big endian), left justified  */
653         /* obj->rx_msg.id = be32_to_cpu(readl(msg_addr + BCI_MSG_ID) >> 3); */
654         memcpy_fromio(&tmp32, msg_addr + BCI_MSG_ID, 4);
655         obj->rx_msg.id = be32_to_cpu(tmp32 >> 3);
656         if (len > 0)
657           memcpy_fromio(obj->rx_msg.data, msg_addr + BCI_MSG_EXT_DATA, len);
658       } else
659       {
660         /* standard frame - 11 bit identifier */
661         /* the ID is stored in motorola format (big endian), left justified */
662         /* obj->rx_msg.id = be16_to_cpu(readw(msg_addr + BCI_MSG_ID) >> 5); */
663         memcpy_fromio(&tmp16, msg_addr + BCI_MSG_ID, 2);
664         obj->rx_msg.id = be16_to_cpu(tmp16 >> 5);
665         if (len > 0)
666           memcpy_fromio(obj->rx_msg.data, msg_addr + BCI_MSG_STD_DATA, len);
667       }
668       canque_filter_msg2edges(obj->qends, &obj->rx_msg);
669     }
670     else
671     {
672       /* this is a status message */
673       status = readw(msg_addr + BCI_MSG_CAN_STATUS);
674       DEBUGMSG ("ipci165_irq_read_handler[%i]: CAN status=%04x\n",chip->chip_idx, status);
675
676       /* wake up the reset thread if the CAN is in bus off */
677       if (status & BCI_CAN_STATUS_BUS_OFF) 
678       {
679         CANMSG ("BUS-OFF detected! Restarting\n");
680         set_bit(CHIP_FLAG_BUS_OFF,&chip_data->flags);
681         wake_up(&chip_data->kthread.queue);
682       }
683
684       if(obj->tx_slot)
685       {
686         canque_notify_inends(obj->tx_qedge, CANQUEUE_NOTIFY_ERRTX_BUS);
687       }
688
689     }
690     DEBUGMSG ("ipci165_irq_read_handler[%i]: device status\n", chip->chip_idx);
691     dump_mem(chip->chip_base_addr + OF_STATUS_BUFFER, 12);
692
693     /* update pointer */
694     queue->idx = (queue->idx + 1) % BCI_QUEUE_SIZE;
695     /* release the buffer */
696     writeb(BCI_MSG_STATUS_FREE, msg_addr + BCI_MSG_STATUS);
697     msg_addr = queue_addr + queue->idx * BCI_MSG_SIZE;
698
699   } while (readb(msg_addr + BCI_MSG_STATUS) == BCI_MSG_STATUS_FULL);
700
701 }
702
703 /**
704  * ipci165_irq_write_handler - ISR code responsible for transmitting
705  * @chip: pointer to chip state structure
706  * @obj: pointer to attached queue description
707  *
708  * The main purpose of this function is to read message from attached queues
709  * and transfer message contents into CAN controller chip.
710  * File: src/ipci165.c
711  */
712 void ipci165_irq_write_handler(struct canchip_t *chip, struct msgobj_t *obj)
713 {
714   struct ipci165_chip_t *chip_data = ((struct ipci165_chip_t *)chip->chip_data);
715   struct bci_queue_t *queue      = &chip_data->tx_queue;
716   unsigned long       queue_addr = queue->addr;
717   unsigned long       msg_addr   = queue_addr + queue->idx * BCI_MSG_SIZE;
718   struct canque_slot_t *tx_slot;
719
720   int len;
721   unsigned char frame_info, ext;
722   unsigned short tmp16;
723   unsigned long  tmp32;
724
725   DEBUGMSG ("ipci165_irq_write_handler[%i]\n",chip->chip_idx);
726
727   while ((canque_test_outslot(obj->qends, &obj->tx_qedge, &obj->tx_slot) >=0))
728   {
729     if (test_bit(CHIP_FLAG_RESET,&chip_data->flags) ||
730         (readb(msg_addr + BCI_MSG_STATUS) == BCI_MSG_STATUS_FULL))
731     {
732       canque_again_outslot(obj->qends, obj->tx_qedge, obj->tx_slot);
733
734       /* lost interrupt work around */
735       ipci165_generate_irq(obj->hostchip->hostdevice);
736
737       mod_timer(&obj->tx_timeout, jiffies + ipci165_qfull_latency(obj));
738       DEBUGMSG("ipci165_irq_write_handler[%i]: scheduled retry\n", chip->chip_idx);
739
740       return;
741     }
742
743     tx_slot = obj->tx_slot;
744     DEBUGMSG ("msg[%i] : id=%lx dlc=%x flg=%02x\n",
745               chip->chip_idx,
746               (unsigned long)tx_slot->msg.id,
747               (unsigned int)tx_slot->msg.length,
748               (unsigned int)tx_slot->msg.flags);
749     dump_mem(tx_slot->msg.data, tx_slot->msg.length);
750
751     len = tx_slot->msg.length;
752     if(len > CAN_MSG_LENGTH) len = CAN_MSG_LENGTH;
753
754     ext = tx_slot->msg.flags;
755     frame_info =
756         len |
757         ((tx_slot->msg.flags & MSG_RTR) ? BCI_MSG_FRAME_RTR : 0) |
758         ((tx_slot->msg.flags & MSG_EXT) ? BCI_MSG_FRAME_EXT : 0);
759
760     writeb(BCI_MSG_SIZE - 2, msg_addr + BCI_MSG_NUM);
761     writeb(BCI_MSG_TYPE_CAN, msg_addr + BCI_MSG_TYPE);
762     writeb(frame_info, msg_addr + BCI_MSG_FRAME);
763     if (frame_info & BCI_MSG_FRAME_EXT)
764     {
765       /* extended frame - 29 bit identifier */
766       /* the ID is stored in motorola format (big endian), left justified  */
767       tmp32 = be32_to_cpu(tx_slot->msg.id) << 3;
768       memcpy_toio(msg_addr + BCI_MSG_ID, &tmp32, 4);
769       if (len > 0)
770         memcpy_toio(msg_addr + BCI_MSG_EXT_DATA, tx_slot->msg.data, len);
771     } else
772     {
773       /* standard frame - 11 bit identifier */
774       /* the ID is stored in motorola format (big endian), left justified */
775       tmp16 = be16_to_cpu(tx_slot->msg.id) << 5;
776       memcpy_toio(msg_addr + BCI_MSG_ID, &tmp16, 2);
777       if (len > 0)
778         memcpy_toio(msg_addr + BCI_MSG_STD_DATA, tx_slot->msg.data, len);
779     }
780
781     dump_mem(msg_addr, BCI_MSG_SIZE);
782
783     /* update pointer */
784     queue->idx = (queue->idx + 1) % BCI_QUEUE_SIZE;
785     /* mark the buffer as full */
786     writeb(BCI_MSG_STATUS_FULL, msg_addr + BCI_MSG_STATUS);
787     /* wake up the controller */
788     ipci165_generate_irq(chip->hostdevice);
789
790     /* next message address */
791     msg_addr = queue_addr + queue->idx * BCI_MSG_SIZE;
792
793     /* Do local transmitted message distribution if enabled. */
794     /* This code should not be called directly there, because it breaks strict
795     behavior of queues if O_SYNC is set. */
796     if (processlocal){
797       obj->tx_slot->msg.flags |= MSG_LOCAL;
798       canque_filter_msg2edges(obj->qends, &obj->tx_slot->msg);
799     }
800     /* Free transmitted slot */
801     canque_free_outslot(obj->qends, obj->tx_qedge, obj->tx_slot);
802     obj->tx_slot = NULL;
803   }
804   return;
805 }
806
807 /**
808  * ipci165_irq_sync_activities - Synchronized access to write handler
809  * @chip: pointer to chip state structure
810  * @obj: pointer to attached queue description
811  *
812  * Return Value: The function always returns zero
813  * File: src/ipci165.c
814  */
815 void ipci165_irq_sync_activities(struct canchip_t *chip, struct msgobj_t *obj)
816 {
817   while(!can_msgobj_test_and_set_fl(obj,TX_LOCK)) 
818   {
819     if(can_msgobj_test_and_clear_fl(obj,TX_REQUEST)) 
820     {
821       ipci165_irq_write_handler(chip, obj);
822     }
823
824     can_msgobj_clear_fl(obj,TX_LOCK);
825     if(can_msgobj_test_fl(obj,TX_REQUEST))
826       continue;
827 /*    if(can_msgobj_test_fl(obj,FILTCH_REQUEST) && !obj->tx_slot)
828     continue; */
829     break;
830   }
831 }
832
833 /**
834  * ipci165_irq_chip_handler - ISR for dedicated chip
835  * @chip: pointer to chip state structure
836  *
837  * The main purpose of this function is to perform all necessary channel
838  * operations as a reaction on signalled interrupt. 
839  * File: src/ipci165.c
840  */
841 void ipci165_irq_chip_handler(struct canchip_t *chip)
842 {
843   struct msgobj_t       *obj = chip->msgobj[0];
844   struct ipci165_chip_t *chip_data = chip->chip_data;
845   struct bci_queue_t    *queue; 
846
847   DEBUGMSG ("ipci165_irq_chip_handler[%i]\n",chip->chip_idx);
848
849   /* check receive queue for messages */
850   queue = &chip_data->rx_queue;
851   if (readb(queue->addr + queue->idx * BCI_MSG_SIZE + BCI_MSG_STATUS)
852       == BCI_MSG_STATUS_FULL)
853     ipci165_irq_read_handler(chip, obj);
854
855   queue = &chip_data->tx_queue;
856 /*  if (readb(queue->addr + queue->idx * BCI_MSG_SIZE + BCI_MSG_STATUS)
857   == BCI_MSG_STATUS_FREE) */
858   {
859     can_msgobj_set_fl(obj,TX_REQUEST);
860
861     /* calls unican_irq_write_handler synchronized with other invocations */
862     ipci165_irq_sync_activities(chip, obj);
863   }
864
865 }
866
867 #define MAX_RETR 10
868
869 /**
870  * ipci165_irq_handler - Interrupt service routine
871  * @irq: interrupt vector number, this value is system specific
872  * @dev_id: driver private pointer registered at time of request_irq() call.
873  *      The CAN driver uses this pointer to store relationship of interrupt
874  *      to chip state structure - @struct canchip_t
875  * @regs: system dependent value pointing to registers stored in exception frame
876  * 
877  * The interrupt handler is activated when the ipci165 controller generates
878  * an interrupt as a reaction an internal state change. The interrupt is
879  * acknowledged and ipci165_irq_chip_handler is called for every channel.
880  * File: src/ipci165.c
881  */
882 can_irqreturn_t ipci165_irq_handler(int irq, void *dev_id, struct pt_regs *regs)
883 {
884   int retval;
885   struct candevice_t *candev = (struct candevice_t *)dev_id;
886
887   unsigned long crm_addr   = candev->io_addr;
888   unsigned long ucr1_addr  = crm_addr + CRM_UCR + 1;
889   struct canchip_t *chip;
890   unsigned char icr;
891   int i;
892
893   /* DEBUGMSG ("ipci165_irq_handler\n"); */
894
895   /* read interrupt control register (byte 0) */
896   icr = readb(crm_addr + CRM_ICR);
897
898   if ((icr & 0x44) == 0x44)
899   {
900     DEBUGMSG ("ipci165_irq_handler: pending interrupt\n");
901
902     /* confirm pending interrupt */
903     writeb(readb(ucr1_addr) | 0x01,  ucr1_addr);
904     writeb(readb(ucr1_addr) & ~0x01, ucr1_addr);
905
906     /* call interrupt handler for every channel */
907     for (i=0 ; i < candev->nr_all_chips ; i++)
908     {
909       chip = candev->chip[i];
910       if (chip->flags & CHIP_CONFIGURED)
911         ipci165_irq_chip_handler(candev->chip[i]);
912     }
913     DEBUGMSG ("ipci165_irq_handler: interrupt handled\n");
914
915     retval = CANCHIP_IRQ_HANDLED;
916   } else {
917     DEBUGMSG ("ipci165_irq_handler: not our interrupt\n");
918     retval = CANCHIP_IRQ_NONE;
919   }
920
921   return CAN_IRQ_RETVAL(retval);
922 }
923
924 /**
925  * ipci165_wakeup_tx - Wakeup TX processing
926  * @chip: pointer to chip state structure
927  * @obj: pointer to message object structure
928  *
929  * Function is responsible for initiating message transmition.
930  * It is responsible for clearing of object TX_REQUEST flag
931  *
932  * Return Value: negative value reports error.
933  * File: src/ipci165.c
934  */
935 int ipci165_wakeup_tx(struct canchip_t *chip, struct msgobj_t *obj)
936 {
937   DEBUGMSG ("ipci165_wakeup_tx\n");
938   can_preempt_disable();
939
940   can_msgobj_set_fl(obj,TX_REQUEST);
941
942   /* calls ipci165_irq_write_handler synchronized with other invocations
943   from kernel and IRQ context */
944   ipci165_irq_sync_activities(chip, obj);
945
946   can_preempt_enable();
947   DEBUGMSG ("ipci165_wakeup_tx: finished\n");
948
949   return 0;
950 }
951
952 void ipci165_do_tx_timeout(unsigned long data)
953 {
954   struct msgobj_t *obj=(struct msgobj_t *)data;
955
956   DEBUGMSG ("ipci165_do_tx_timeout\n");
957
958   can_preempt_disable();
959
960   can_msgobj_set_fl(obj,TX_REQUEST);
961
962   /* calls ipci165_irq_write_handler synchronized with other invocations
963   from kernel and IRQ context */
964   ipci165_irq_sync_activities(obj->hostchip, obj);
965
966   can_preempt_enable();
967   DEBUGMSG ("ipci165_do_tx_timeout: finished\n");
968 }
969
970 /* * * iPC-I 165/PCI Board Functionality * * */
971
972 /**
973  * ipci165_request_io - Reserve io or memory range for can board
974  * @candev: pointer to candevice/board which asks for io. Field @io_addr
975  *      of @candev is used in most cases to define start of the range
976  *
977  * Return Value: The function returns zero on success or %-ENODEV on failure
978  * File: src/ipci165.c
979  */
980 int ipci165_request_io(struct candevice_t *candev)
981 {
982   unsigned long dpram_addr;
983   unsigned long crm_addr;
984   unsigned long fix_addr;
985   int i,j;
986
987   DEBUGMSG ("ipci165_request_io\n");
988
989   crm_addr   = pci_resource_start(candev->sysdevptr.pcidev,0);
990   dpram_addr = pci_resource_start(candev->sysdevptr.pcidev,2);
991
992   DEBUGMSG ("ipci165_request_io: crm = 0x%lx, dpram = 0x%lx\n",crm_addr, dpram_addr);
993
994   /* verify, if our HW is buggy, and try to fix it */
995 #if 0
996   if (test_bit (7, &crm_addr))
997   {
998     CANMSG ("Wrong PCI base address [0x%lx](PLX PCI9050 bug)!\n", dpram_addr);
999
1000     fix_addr = pci_resource_start(candev->sysdevptr.pcidev,3);
1001
1002     if (fix_addr == 0)
1003     {
1004       CANMSG ("This card was not fixed!\n");
1005
1006       if (candev->io_addr == 0)
1007       {
1008         CANMSG ("You have to specify IO address parameter!\n");
1009         return -EINVAL;
1010       }
1011       CANMSG ("Using specified IO address value for the memory [0x%lx]\n",
1012               candev->io_addr);
1013     }
1014     else
1015     {
1016       CANMSG ("Fixed card. Using of 3 region [0x%lx]\n", fix_addr);
1017       candev->io_addr = fix_addr;
1018     }
1019
1020     pci_write_config_dword (candev->sysdevptr.pcidev,
1021                             PCI_BASE_ADDRESS_0, fix_addr);
1022   }
1023 #endif
1024
1025 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,4,21))
1026   if(pci_request_region(candev->sysdevptr.pcidev, 2, "kv_ipci165_dpram") == 0)
1027   {
1028     if(pci_request_region(candev->sysdevptr.pcidev, 0, "kv_ipci165_reg") == 0)
1029     {
1030 #else /*(LINUX_VERSION_CODE > KERNEL_VERSION(2,4,21))*/
1031   if(pci_request_regions(candev->sysdevptr.pcidev, "kv_ipci165") == 0)
1032   {
1033 #endif /*(LINUX_VERSION_CODE > KERNEL_VERSION(2,4,21))*/
1034
1035       if ((candev->dev_base_addr = (long) ioremap(dpram_addr, 
1036            pci_resource_len(candev->sysdevptr.pcidev,2))))
1037       {
1038         DEBUGMSG ("ipci165_request_io: dpram remapped to 0x%lx\n", candev->dev_base_addr);
1039
1040         if ((candev->io_addr = (long) ioremap(crm_addr, 
1041              pci_resource_len(candev->sysdevptr.pcidev,0))))
1042         {
1043           DEBUGMSG ("ipci165_request_io: crm remapped to 0x%lx\n", candev->io_addr);
1044           /* all resources has been allocated */
1045           candev->res_addr=candev->io_addr;
1046
1047           /* Because of my mapping, I cannot use the
1048              can_base_addr_fixup(candev, remap_addr) to remap the addresses */
1049           for(i=0;i<candev->nr_all_chips;i++)
1050           {
1051             candev->chip[i]->chip_base_addr = candev->dev_base_addr;
1052             for(j=0;j<candev->chip[i]->max_objects;j++)
1053               candev->chip[i]->msgobj[j]->obj_base_addr = candev->dev_base_addr;
1054           }
1055
1056           return 0;
1057
1058         } else CANMSG("Unable to remap memory at: 0x%lx\n", crm_addr);
1059         iounmap((void*)candev->io_addr);
1060
1061       } else CANMSG("Unable to remap memory at: 0x%lx\n", dpram_addr);
1062       iounmap((void*)candev->dev_base_addr);
1063
1064 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,4,21))
1065       pci_release_region(candev->sysdevptr.pcidev, 0);
1066     } else CANMSG("Request of kv_ipci165_reg range failed\n");
1067
1068     pci_release_region(candev->sysdevptr.pcidev, 2);
1069   } else CANMSG("Request of kv_ipci165_dpram range failed\n");
1070
1071 #else /*(LINUX_VERSION_CODE > KERNEL_VERSION(2,4,21))*/
1072     pci_release_regions(candev->sysdevptr.pcidev);
1073   } else CANMSG("Request of kv_ipci165 regions failed\n");
1074 #endif /*(LINUX_VERSION_CODE > KERNEL_VERSION(2,4,21))*/
1075
1076   return -ENODEV;
1077 }
1078
1079 /**
1080  * ipci165_release_io - Free reserved io memory range
1081  * @candev: pointer to candevice/board which releases io
1082  *
1083  * Return Value: The function always returns zero
1084  * File: src/ipci165.c
1085  */
1086 int ipci165_release_io(struct candevice_t *candev)
1087 {
1088   struct ipci165_chip_t *chip_data;
1089   int i;
1090   
1091   /* disable irq on HW */
1092   ipci165_disconnect_irq(candev);
1093
1094 #if 0
1095   /* terminate the kernel threads */
1096   for (i = 0 ; i < candev->nr_all_chips ; i++)
1097   {
1098     chip_data = (struct ipci165_chip_t *)candev->chip[i]->chip_data;
1099     stop_kthread(&chip_data->restart_thread);
1100   }
1101 #endif
1102
1103   iounmap((void*)candev->io_addr);
1104   iounmap((void*)candev->dev_base_addr);
1105
1106 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,4,21))
1107   pci_release_region(candev->sysdevptr.pcidev, 2);
1108   pci_release_region(candev->sysdevptr.pcidev, 0);
1109 #else /*(LINUX_VERSION_CODE > KERNEL_VERSION(2,4,21))*/
1110   pci_release_regions(candev->sysdevptr.pcidev);
1111 #endif /*(LINUX_VERSION_CODE > KERNEL_VERSION(2,4,21))*/
1112
1113   return 0;
1114 }
1115
1116 /**
1117  * ipci165_download_fw - Download FW into CAN hardware
1118  * @candev: Pointer to candevice/board structure
1119  *
1120  * Return Value: returns zero on success
1121  * File: src/ipci165.c
1122  */
1123 int ipci165_download_fw(struct candevice_t *candev)
1124 {
1125   unsigned long dpram_addr = candev->dev_base_addr;
1126   char board_name[BOARD_NAME_LEN+1];
1127   char hw_version[HW_VERSION_LEN+1];
1128   char mode[MODE_LEN+1];
1129
1130   struct ipci165_fw_t *fwArray = ipci165_fw;
1131   int attempt;
1132
1133   DEBUGMSG ("ipci165_download_fw\n");
1134
1135   /* read name and version */  
1136   memcpy_fromio (board_name, dpram_addr + BOARD_NAME_OFS, BOARD_NAME_LEN);
1137   board_name[BOARD_NAME_LEN] = 0;
1138
1139   memcpy_fromio (hw_version, dpram_addr + HW_VERSION_OFS, HW_VERSION_LEN);
1140   hw_version[HW_VERSION_LEN] = 0;
1141
1142   CANMSG ("Board Name: %s\n", board_name);
1143   CANMSG ("HW Version: %s\n", hw_version);
1144
1145 /*
1146   if ((hw_version[0] != 'V') && (hw_version[0] != 'v'))
1147 {
1148   CANMSG ("This board is too old and not supported by the BCI !\n");
1149   return -ENODEV;
1150 }
1151 */
1152
1153   /* detect & test mode */
1154   memcpy_fromio (mode, dpram_addr + MODE_OFS, MODE_LEN);
1155   mode[MODE_LEN] = 0;
1156
1157   if (strncmp (mode, "PC-Loader V", 11))
1158   {
1159     CANMSG ("Unknown mode [%s], can't download firmware!\n",mode);
1160     return -ENODEV;
1161   }
1162
1163   while (fwArray->len)
1164   {
1165     /* fill buffer */
1166     writeb(LD_CMD_DOWNLOAD, dpram_addr + OF_LD_CMD);
1167     writeb(fwArray->len, dpram_addr + OF_LD_NUM);
1168     writeb(0, dpram_addr + OF_LD_NUM + 1);
1169
1170     writel(fwArray->addr, dpram_addr + OF_LD_ADDRESS);
1171     /*    writel already performes the cpu_to_le32 conversion by itself   */
1172     /*    writel(cpu_to_le32(fwArray->addr), dpram_addr + OF_LD_ADDRESS); */
1173
1174     memcpy_toio(dpram_addr + OF_LD_DATA, fwArray->a_data, fwArray->len);
1175
1176 #if 0
1177     dump_mem((void *)(dpram_addr + OF_LD_SYNC), fwArray->len + 8);
1178 #endif
1179     /* buffer is prepared, set flag for loader */
1180     writeb(1, dpram_addr + OF_LD_SYNC);
1181
1182     /* update pointer */
1183     fwArray++;
1184
1185     /* wait for the loader */
1186     attempt = 1000;
1187     while (readb(dpram_addr + OF_LD_SYNC) != 0)
1188     {
1189       udelay(100);
1190       if (--attempt == 0)
1191       {
1192         /* timeout occured */
1193         CANMSG ("Firmware download failed!\n");
1194         return -ENODEV;
1195       }
1196     }
1197   }
1198   CANMSG ("Firmware downladed successfully\n");
1199
1200   /* start the FW */
1201   writeb(LD_CMD_START_FW, dpram_addr + OF_LD_CMD);
1202   writeb(1, dpram_addr + OF_LD_SYNC);
1203   ipci165_delay (500);
1204
1205   return 0;
1206 }
1207
1208 /**
1209  * ipci165_reset - Hardware reset routine
1210  * @candev: Pointer to candevice/board structure
1211  *
1212  * Return Value: The function returns zero on success or %-ENODEV on failure
1213  * File: src/ipci165.c
1214  */
1215 int ipci165_reset(struct candevice_t *candev)
1216 {
1217   unsigned long crm_addr = candev->io_addr;
1218   unsigned long test_data;
1219   char buffer[BCI_CMD_MAX_LEN];
1220   int i, size, chips;
1221   unsigned char ucr;
1222   struct canchip_t *chip;
1223   struct ipci165_chip_t *chip_data;
1224
1225   DEBUGMSG ("ipci165_reset: hardware reset\n");
1226
1227   /* reset the HW */
1228   ucr = readb(crm_addr + CRM_UCR + 3);
1229   writeb(ucr | 0x40, crm_addr + CRM_UCR + 3);
1230   udelay(100);
1231   writeb(ucr & ~0x40, crm_addr + CRM_UCR + 3);
1232
1233   /* wait a little bit */
1234   ipci165_delay(200);
1235
1236   /* download FW */
1237   if (ipci165_download_fw(candev)) return -ENODEV;
1238
1239   /* enable irq on HW */
1240   if (ipci165_connect_irq(candev))
1241     {
1242     CANMSG ("Interrupt routine installation for IRQ %i failed!\n",
1243             candev->sysdevptr.pcidev->irq);
1244     return -ENODEV;
1245     }
1246
1247   /* test BCI interface */
1248   test_data = 0x12345678;
1249   size = sizeof(test_data);
1250   if (bci_command(candev, CMD_TEST, size, (char *)&test_data) ||
1251       bci_response(candev, CMD_TEST, &size, (char *)&test_data) ||
1252       (test_data != ~0x12345678))
1253   {
1254     CANMSG ("BCI test failed! Test pattern is %lx\n", test_data);
1255     return -ENODEV;
1256   }
1257
1258   /* get Firmware identification */
1259   /* send command, fw requests 1 dummy byte */
1260   size = BCI_CMD_MAX_LEN;
1261   if (bci_command(candev, CMD_ID, 1, (char *)&test_data) ||
1262       bci_response(candev, CMD_ID, &size, buffer))
1263   {
1264     CANMSG ("Firmware Identification reading failed!\n");
1265     return -ENODEV;
1266   }
1267   CANMSG ("Firmware: %s\n",buffer);
1268
1269   /* get Firmware version */
1270   /* send command, fw requests 1 dummy byte */
1271   size = BCI_CMD_MAX_LEN;
1272   if (bci_command(candev, CMD_VERSION, 1, (char *)&test_data) ||
1273       bci_response(candev, CMD_VERSION, &size, buffer))
1274   {
1275     CANMSG ("Firmware Version reading failed!\n");
1276     return -ENODEV;
1277   }
1278   CANMSG ("Version: %s\n",buffer);
1279
1280   /* get Board Info */
1281   /* send command, fw requests 1 dummy byte */
1282   size = BOARD_INFO_SIZE;
1283   if (bci_command(candev, CMD_GET_BOARD_INFO, 1, (char *)&test_data) ||
1284       bci_response(candev, CMD_GET_BOARD_INFO, &size, (char *) buffer))
1285   {
1286     CANMSG ("Get Board Info failed!\n");
1287     return -ENODEV;
1288   }
1289
1290   chips = le16_to_cpu(*(unsigned short*)(buffer+OF_BOARD_INFO_CHIPS));
1291   /* shouldn't be, but who knows ... */
1292   if (chips > 2) chips = 2;
1293
1294   CANMSG ("Chips: %i\n",chips);
1295   CANMSG ("Chip 1 Type: %s\n",buffer+OF_BOARD_INFO_CHIP1_TYPE);
1296
1297   /* update board info */
1298   if (chips == 1)
1299   {
1300     /* we have to correct the number in candev and release allocated
1301        structures */
1302     candev->nr_all_chips = chips;
1303     canchip_done(candev->chip[1]);
1304
1305   } else CANMSG ("Chip 2 Type: %s\n",buffer+OF_BOARD_INFO_CHIP2_TYPE); 
1306
1307   /* start kernel threads */
1308   for (i = 0 ; i < chips ; i++)
1309   {
1310     chip = candev->chip[i];
1311     chip_data = (struct ipci165_chip_t *)chip->chip_data;
1312     chip_data->kthread.arg = chip;
1313     start_kthread(ipci165_kthread, &chip_data->kthread);
1314   }
1315
1316   CANMSG ("HW is up and working.\n");
1317   return 0;
1318 }
1319
1320 /**
1321  * ipci165_init_hw_data - Initialize hardware cards
1322  * @candev: Pointer to candevice/board structure
1323  *
1324  * Return Value: The function always returns zero
1325  * File: src/ipci165.c
1326  */
1327 int ipci165_init_hw_data(struct candevice_t *candev)
1328 {
1329   struct pci_dev *pcidev = NULL;
1330   unsigned short SubsystemID;
1331
1332   DEBUGMSG ("ipci165_init_hw_data\n");
1333
1334   /* find iPC-I 165 on PCI bus */
1335   do
1336   {
1337     pcidev = pci_find_device(IPCI165_VENDOR_ID, IPCI165_DEVICE_ID, pcidev);
1338     if(pcidev == NULL) return -ENODEV;
1339
1340     /* check subvendor ID */
1341     pci_read_config_word (pcidev, PCI_SUBSYSTEM_ID, &SubsystemID);
1342     if ((SubsystemID != IPCI165_SUBSYSTEM_ID) &&
1343         (SubsystemID != CP350_SUBSYSTEM_ID))
1344       break;
1345   }
1346   while(can_check_dev_taken(pcidev));
1347
1348   /* enable it */
1349   if (pci_enable_device (pcidev))
1350   {
1351     CANMSG ("Cannot enable PCI device\n");
1352     return -EIO;
1353   }
1354
1355   candev->sysdevptr.pcidev = pcidev;
1356   candev->res_addr=0;
1357   candev->nr_82527_chips=0;
1358   candev->nr_sja1000_chips=0;
1359   /* we do not know yet, whether our HW has one or two chan chips. Let's
1360      prepare configuration for maximal configuration = 2. This will be
1361      corrected later on */
1362   candev->nr_all_chips=2; 
1363   candev->flags |= CANDEV_PROGRAMMABLE_IRQ*0;
1364   /* initialize device spinlock */
1365   can_spin_lock_init(candev->device_lock);
1366
1367   return 0;
1368 }
1369
1370 #define CHIP_TYPE "ipci165"
1371
1372 /**
1373  * ipci165_init_chip_data - Initialize chips
1374  * @candev: Pointer to candevice/board structure)
1375  * @chipnr: Number of the CAN chip on the hardware card
1376  *
1377  * Return Value: The function always returns zero
1378  * File: src/ipci165.c
1379  */
1380 int ipci165_init_chip_data(struct candevice_t *candev, int chipnr)
1381 {
1382   struct canchip_t      *chip = candev->chip[chipnr];
1383   struct ipci165_chip_t *chip_data;
1384
1385   DEBUGMSG ("ipci165_init_chip_data\n");
1386
1387   chip->chip_type = CHIP_TYPE;
1388   chip->chip_base_addr = 0; /* mapping not known yet */
1389   chip->clock = 10000000;
1390   chip->int_clk_reg = 0x0;
1391   chip->int_bus_reg = 0x0;
1392   chip->max_objects = 1;
1393
1394 #if 0
1395   /* initialize interrupt handling only for channel 0. The interrupt
1396      is shared between the channels so we have to work it out in one
1397      interrupt routine. */
1398   if (chipnr == 0)
1399   {
1400     chip->chipspecops->irq_handler=ipci165_irq_handler;
1401     chip->chip_irq=candev->sysdevptr.pcidev->irq;
1402     chip->flags |= CHIP_IRQ_PCI;
1403   } else
1404   {
1405     chip->chipspecops->irq_handler=NULL;
1406   }
1407 #else
1408   chip->chipspecops->irq_handler = NULL;
1409   chip->chip_irq = 0;
1410   chip->flags |= CHIP_IRQ_CUSTOM;
1411 #endif
1412
1413   chip_data = can_checked_malloc(sizeof(struct ipci165_chip_t));
1414   if(!chip_data) return -ENOMEM;
1415   chip_data->rx_queue.idx = 0;
1416   chip_data->rx_queue.addr = 0;
1417   chip_data->tx_queue.idx = 0;
1418   chip_data->tx_queue.addr = 0;
1419   chip->chip_data = chip_data;
1420
1421   CANMSG("initializing ipci165 chip operations\n");
1422   chip->chipspecops->chip_config=ipci165_chip_config;
1423   chip->chipspecops->baud_rate=ipci165_baud_rate;
1424   chip->chipspecops->set_btregs=ipci165_set_btregs;
1425   chip->chipspecops->start_chip=ipci165_start_chip;
1426   chip->chipspecops->stop_chip=ipci165_stop_chip;
1427   chip->chipspecops->pre_read_config=ipci165_pre_read_config;
1428   chip->chipspecops->wakeup_tx=ipci165_wakeup_tx;
1429   chip->chipspecops->filtch_rq=NULL;
1430   chip->chipspecops->irq_accept=NULL;
1431
1432   chip->chipspecops->standard_mask=NULL;
1433   chip->chipspecops->extended_mask=NULL;
1434   chip->chipspecops->message15_mask=NULL;
1435   chip->chipspecops->clear_objects=NULL;
1436   chip->chipspecops->config_irqs=NULL;
1437   chip->chipspecops->pre_write_config=NULL;
1438   chip->chipspecops->send_msg=NULL;
1439   chip->chipspecops->check_tx_stat=NULL;
1440   chip->chipspecops->remote_request=NULL;
1441   chip->chipspecops->enable_configuration=NULL;
1442   chip->chipspecops->disable_configuration=NULL;
1443
1444   return 0;
1445 }
1446
1447 /**
1448  * ipci165_init_obj_data - Initialize message buffers
1449  * @chip: Pointer to chip specific structure
1450  * @objnr: Number of the message buffer
1451  *
1452  * Return Value: The function always returns zero
1453  * File: src/ipci165.c
1454  */
1455 int ipci165_init_obj_data(struct canchip_t *chip, int objnr)
1456 {
1457   struct msgobj_t *obj=chip->msgobj[objnr];
1458   
1459   DEBUGMSG ("ipci165_init_obj_data\n");
1460   
1461   obj->obj_base_addr = 0; /* not known yet */
1462   obj->tx_timeout.function = ipci165_do_tx_timeout;
1463   obj->tx_timeout.data = (unsigned long)obj;
1464   return 0;
1465 }
1466
1467 /**
1468  * ipci165_program_irq - Program interrupts
1469  * @candev: Pointer to candevice/board structure
1470  *
1471  * Return value: The function returns zero on success or %-ENODEV on failure
1472  * File: src/ipci165.c
1473  */
1474 int ipci165_program_irq(struct candevice_t *candev)
1475 {
1476   return 0;
1477 }
1478
1479 /**
1480  * ipci165_register - Register Board Support Functions
1481  * @candev: Pointer to hardware/board specific functions
1482  *
1483  * Return value: The function returns zero on success or %-ENODEV on failure
1484  * File: src/ipci165.c
1485  */
1486 int ipci165_register(struct hwspecops_t *hwspecops)
1487 {
1488   hwspecops->request_io = ipci165_request_io;
1489   hwspecops->release_io = ipci165_release_io;
1490   hwspecops->reset = ipci165_reset;
1491   hwspecops->init_hw_data = ipci165_init_hw_data;
1492   hwspecops->init_chip_data = ipci165_init_chip_data;
1493   hwspecops->init_obj_data = ipci165_init_obj_data;
1494   hwspecops->write_register = NULL;
1495   hwspecops->read_register = NULL;
1496   hwspecops->program_irq = ipci165_program_irq;
1497   return 0;
1498 }
1499
1500 #ifdef CAN_DEBUG
1501 void dump_mem(char *ptr, int size)
1502 {
1503   int to, j;
1504   unsigned char str[80], buf[16];
1505   char *strp;
1506
1507   for (; size > 0; size -= 16)
1508   {
1509     to = size > 16 ? 16 : size;
1510     memcpy (buf,ptr, to);
1511     strp = str;
1512     for (j = 0; j < to ; j++)
1513       strp += sprintf(strp, "%02x ",buf[j]);
1514     for (; j < 16 ; j++) 
1515       strp += sprintf(strp, "   ");
1516     for (j = 0; j < to ; j++)
1517       *strp++= isprint(buf[j]) ? buf[j] : '.';
1518
1519     DEBUGMSG ("[%lx] %s\n", (long unsigned)ptr, str);
1520     ptr += to;
1521   }
1522 }
1523 #endif