]> rtime.felk.cvut.cz Git - lincan.git/blob - lincan/src/ipci165.c
The first round of I/O space pointers separation.
[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(CAN_IRQ_HANDLER_ARGS(irq_number, dev_id));
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   can_ioptr_t crm_addr = candev->aux_base_addr;
66   can_writeb(can_readb(crm_addr + CRM_UCR) & 0xFB, crm_addr + CRM_UCR);
67   can_writeb(can_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   can_ioptr_t 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 (can_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   can_writeb(cmd, dpram_addr + OF_BCI_CMD);
106   can_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   can_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   can_ioptr_t 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 (can_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 = can_readb(dpram_addr + OF_BCI_NUM) - 1;
154   if (*size > tmp) *size = tmp;
155
156   if (can_readb(dpram_addr + OF_BCI_CMD) != cmd)
157   {
158     /* release the buffer */
159     can_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   can_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     can_writeb(BCI_MSG_STATUS_FREE, chip_data->rx_queue.addr + msg_ofs);
210     can_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   can_ioptr_t crm_addr = candev->aux_base_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   can_writeb(can_readb(crm_addr + CRM_ICR) | 0x40, crm_addr + CRM_ICR);
331   udelay (100);
332   icr = can_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   can_ioptr_t crm_addr = candev->aux_base_addr;
346   unsigned char icr;
347   DEBUGMSG ("ipci165_disconnect_irq\n");
348
349   // Enable interrupt to PC
350   can_writeb(can_readb(crm_addr + CRM_ICR) & ~0x40, crm_addr + CRM_ICR);
351   udelay (100);
352   icr = can_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   can_ioptr_t         queue_addr = queue->addr;
611   can_ioptr_t         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 (can_readb(msg_addr + BCI_MSG_TYPE) == BCI_MSG_TYPE_CAN)
624     {
625 #if 0
626       printk("ST(0)=%x, ST(1)=%x\n",can_readw(chip->chip_base_addr+OF_CAN1_STATUS),
627              can_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,can_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 = can_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 * can_readl(msg_addr + BCI_MSG_TIMESTAMP);
643       /*  BCI_TIMESTAMP_RES * le32_to_cpu(can_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(can_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(can_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 = can_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     can_writeb(BCI_MSG_STATUS_FREE, msg_addr + BCI_MSG_STATUS);
697     msg_addr = queue_addr + queue->idx * BCI_MSG_SIZE;
698
699   } while (can_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   can_ioptr_t         queue_addr = queue->addr;
717   can_ioptr_t         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         (can_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     can_writeb(BCI_MSG_SIZE - 2, msg_addr + BCI_MSG_NUM);
761     can_writeb(BCI_MSG_TYPE_CAN, msg_addr + BCI_MSG_TYPE);
762     can_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     can_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 (can_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 (can_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(CAN_IRQ_HANDLER_ARGS(irq_number, dev_id))
883 {
884   int retval;
885   struct candevice_t *candev = (struct candevice_t *)dev_id;
886
887   can_ioptr_t crm_addr   = candev->aux_base_addr;
888   can_ioptr_t 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 = can_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     can_writeb(can_readb(ucr1_addr) | 0x01,  ucr1_addr);
904     can_writeb(can_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 /**
971  * ipci165_attach_to_chip: - attaches to the chip, setups registers and state
972  * @chip: pointer to chip state structure
973  *
974  * Return Value: negative value reports error.
975  * File: src/ipci165.c
976  */
977 int ipci165_attach_to_chip(struct canchip_t *chip)
978 {
979   return 0;
980 }
981
982 /**
983  * ipci165_release_chip: - called before chip structure removal if %CHIP_ATTACHED is set
984  * @chip: pointer to chip state structure
985  *
986  * Return Value: negative value reports error.
987  * File: src/ipci165.c
988  */
989 int ipci165_release_chip(struct canchip_t *chip)
990 {
991   ipci165_stop_chip(chip);
992   /* disable interrupts in the hardware, etc. */
993   return 0;
994 }
995
996 /* * * iPC-I 165/PCI Board Functionality * * */
997
998 /**
999  * ipci165_request_io - Reserve io or memory range for can board
1000  * @candev: pointer to candevice/board which asks for io. Field @io_addr
1001  *      of @candev is used in most cases to define start of the range
1002  *
1003  * Return Value: The function returns zero on success or %-ENODEV on failure
1004  * File: src/ipci165.c
1005  */
1006 int ipci165_request_io(struct candevice_t *candev)
1007 {
1008   unsigned long dpram_addr; /* physical address before remap for this function */
1009   unsigned long crm_addr;   /* physical address before remap for this function */
1010   unsigned long fix_addr;   /* physical address before remap for this function */
1011   int i,j;
1012
1013   DEBUGMSG ("ipci165_request_io\n");
1014
1015   crm_addr   = pci_resource_start(candev->sysdevptr.pcidev,0);
1016   dpram_addr = pci_resource_start(candev->sysdevptr.pcidev,2);
1017
1018   DEBUGMSG ("ipci165_request_io: crm = 0x%lx, dpram = 0x%lx\n",crm_addr, dpram_addr);
1019
1020   /* verify, if our HW is buggy, and try to fix it */
1021 #if 0
1022   if (test_bit (7, &crm_addr))
1023   {
1024     CANMSG ("Wrong PCI base address [0x%lx](PLX PCI9050 bug)!\n", dpram_addr);
1025
1026     fix_addr = pci_resource_start(candev->sysdevptr.pcidev,3);
1027
1028     if (fix_addr == 0)
1029     {
1030       CANMSG ("This card was not fixed!\n");
1031
1032       if (candev->aux_base_addr == NULL)
1033       {
1034         CANMSG ("You have to specify IO address parameter!\n");
1035         return -EINVAL;
1036       }
1037       CANMSG ("Using specified IO address value for the memory [0x%lx]\n",
1038               can_ioptr2ulong(candev->aux_base_addr));
1039     }
1040     else
1041     {
1042       CANMSG ("Fixed card. Using of 3 region [0x%lx]\n", fix_addr);
1043       candev->aux_base_addr = fix_addr;
1044     }
1045
1046     pci_write_config_dword (candev->sysdevptr.pcidev,
1047                             PCI_BASE_ADDRESS_0, fix_addr);
1048   }
1049 #endif
1050
1051 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,4,21))
1052   if(pci_request_region(candev->sysdevptr.pcidev, 2, "kv_ipci165_dpram") == 0)
1053   {
1054     if(pci_request_region(candev->sysdevptr.pcidev, 0, "kv_ipci165_reg") == 0)
1055     {
1056 #else /*(LINUX_VERSION_CODE > KERNEL_VERSION(2,4,21))*/
1057   if(pci_request_regions(candev->sysdevptr.pcidev, "kv_ipci165") == 0)
1058   {
1059 #endif /*(LINUX_VERSION_CODE > KERNEL_VERSION(2,4,21))*/
1060
1061       if ((candev->dev_base_addr = ioremap(dpram_addr, 
1062            pci_resource_len(candev->sysdevptr.pcidev,2))))
1063       {
1064         DEBUGMSG ("ipci165_request_io: dpram remapped to 0x%lx\n", candev->dev_base_addr);
1065
1066         if ((candev->aux_base_addr = ioremap(crm_addr, 
1067              pci_resource_len(candev->sysdevptr.pcidev,0))))
1068         {
1069           DEBUGMSG ("ipci165_request_io: crm remapped to 0x%lx\n", can_ioptr2ulong(candev->aux_base_addr));
1070           /* all resources has been allocated */
1071
1072           /* Because of my mapping, I cannot use the
1073              can_base_addr_fixup(candev, remap_addr) to remap the addresses */
1074           for(i=0;i<candev->nr_all_chips;i++)
1075           {
1076             candev->chip[i]->chip_base_addr = candev->dev_base_addr;
1077             for(j=0;j<candev->chip[i]->max_objects;j++)
1078               candev->chip[i]->msgobj[j]->obj_base_addr = candev->dev_base_addr;
1079           }
1080
1081           return 0;
1082
1083         } else CANMSG("Unable to remap memory at: 0x%lx\n", crm_addr);
1084         iounmap(candev->aux_base_addr);
1085
1086       } else CANMSG("Unable to remap memory at: 0x%lx\n", dpram_addr);
1087       iounmap(candev->dev_base_addr);
1088
1089 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,4,21))
1090       pci_release_region(candev->sysdevptr.pcidev, 0);
1091     } else CANMSG("Request of kv_ipci165_reg range failed\n");
1092
1093     pci_release_region(candev->sysdevptr.pcidev, 2);
1094   } else CANMSG("Request of kv_ipci165_dpram range failed\n");
1095
1096 #else /*(LINUX_VERSION_CODE > KERNEL_VERSION(2,4,21))*/
1097     pci_release_regions(candev->sysdevptr.pcidev);
1098   } else CANMSG("Request of kv_ipci165 regions failed\n");
1099 #endif /*(LINUX_VERSION_CODE > KERNEL_VERSION(2,4,21))*/
1100
1101   return -ENODEV;
1102 }
1103
1104 /**
1105  * ipci165_release_io - Free reserved io memory range
1106  * @candev: pointer to candevice/board which releases io
1107  *
1108  * Return Value: The function always returns zero
1109  * File: src/ipci165.c
1110  */
1111 int ipci165_release_io(struct candevice_t *candev)
1112 {
1113   struct ipci165_chip_t *chip_data;
1114   int i;
1115   
1116   /* disable irq on HW */
1117   ipci165_disconnect_irq(candev);
1118
1119 #if 0
1120   /* terminate the kernel threads */
1121   for (i = 0 ; i < candev->nr_all_chips ; i++)
1122   {
1123     chip_data = (struct ipci165_chip_t *)candev->chip[i]->chip_data;
1124     stop_kthread(&chip_data->restart_thread);
1125   }
1126 #endif
1127
1128   iounmap(candev->aux_base_addr);
1129   iounmap(candev->dev_base_addr);
1130
1131 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,4,21))
1132   pci_release_region(candev->sysdevptr.pcidev, 2);
1133   pci_release_region(candev->sysdevptr.pcidev, 0);
1134 #else /*(LINUX_VERSION_CODE > KERNEL_VERSION(2,4,21))*/
1135   pci_release_regions(candev->sysdevptr.pcidev);
1136 #endif /*(LINUX_VERSION_CODE > KERNEL_VERSION(2,4,21))*/
1137
1138   return 0;
1139 }
1140
1141 /**
1142  * ipci165_download_fw - Download FW into CAN hardware
1143  * @candev: Pointer to candevice/board structure
1144  *
1145  * Return Value: returns zero on success
1146  * File: src/ipci165.c
1147  */
1148 int ipci165_download_fw(struct candevice_t *candev)
1149 {
1150   can_ioptr_t dpram_addr = candev->dev_base_addr;
1151   char board_name[BOARD_NAME_LEN+1];
1152   char hw_version[HW_VERSION_LEN+1];
1153   char mode[MODE_LEN+1];
1154
1155   struct ipci165_fw_t *fwArray = ipci165_fw;
1156   int attempt;
1157
1158   DEBUGMSG ("ipci165_download_fw\n");
1159
1160   /* read name and version */  
1161   memcpy_fromio (board_name, dpram_addr + BOARD_NAME_OFS, BOARD_NAME_LEN);
1162   board_name[BOARD_NAME_LEN] = 0;
1163
1164   memcpy_fromio (hw_version, dpram_addr + HW_VERSION_OFS, HW_VERSION_LEN);
1165   hw_version[HW_VERSION_LEN] = 0;
1166
1167   CANMSG ("Board Name: %s\n", board_name);
1168   CANMSG ("HW Version: %s\n", hw_version);
1169
1170 /*
1171   if ((hw_version[0] != 'V') && (hw_version[0] != 'v'))
1172 {
1173   CANMSG ("This board is too old and not supported by the BCI !\n");
1174   return -ENODEV;
1175 }
1176 */
1177
1178   /* detect & test mode */
1179   memcpy_fromio (mode, dpram_addr + MODE_OFS, MODE_LEN);
1180   mode[MODE_LEN] = 0;
1181
1182   if (strncmp (mode, "PC-Loader V", 11))
1183   {
1184     CANMSG ("Unknown mode [%s], can't download firmware!\n",mode);
1185     return -ENODEV;
1186   }
1187
1188   while (fwArray->len)
1189   {
1190     /* fill buffer */
1191     can_writeb(LD_CMD_DOWNLOAD, dpram_addr + OF_LD_CMD);
1192     can_writeb(fwArray->len, dpram_addr + OF_LD_NUM);
1193     can_writeb(0, dpram_addr + OF_LD_NUM + 1);
1194
1195     can_writel(fwArray->addr, dpram_addr + OF_LD_ADDRESS);
1196     /*    can_writel already performes the cpu_to_le32 conversion by itself   */
1197     /*    can_writel(cpu_to_le32(fwArray->addr), dpram_addr + OF_LD_ADDRESS); */
1198
1199     memcpy_toio(dpram_addr + OF_LD_DATA, fwArray->a_data, fwArray->len);
1200
1201 #if 0
1202     dump_mem((void *)(dpram_addr + OF_LD_SYNC), fwArray->len + 8);
1203 #endif
1204     /* buffer is prepared, set flag for loader */
1205     can_writeb(1, dpram_addr + OF_LD_SYNC);
1206
1207     /* update pointer */
1208     fwArray++;
1209
1210     /* wait for the loader */
1211     attempt = 1000;
1212     while (can_readb(dpram_addr + OF_LD_SYNC) != 0)
1213     {
1214       udelay(100);
1215       if (--attempt == 0)
1216       {
1217         /* timeout occured */
1218         CANMSG ("Firmware download failed!\n");
1219         return -ENODEV;
1220       }
1221     }
1222   }
1223   CANMSG ("Firmware downladed successfully\n");
1224
1225   /* start the FW */
1226   can_writeb(LD_CMD_START_FW, dpram_addr + OF_LD_CMD);
1227   can_writeb(1, dpram_addr + OF_LD_SYNC);
1228   ipci165_delay (500);
1229
1230   return 0;
1231 }
1232
1233 /**
1234  * ipci165_reset - Hardware reset routine
1235  * @candev: Pointer to candevice/board structure
1236  *
1237  * Return Value: The function returns zero on success or %-ENODEV on failure
1238  * File: src/ipci165.c
1239  */
1240 int ipci165_reset(struct candevice_t *candev)
1241 {
1242   can_ioptr_t crm_addr = candev->aux_base_addr;
1243   unsigned long test_data;
1244   char buffer[BCI_CMD_MAX_LEN];
1245   int i, size, chips;
1246   unsigned char ucr;
1247   struct canchip_t *chip;
1248   struct ipci165_chip_t *chip_data;
1249
1250   DEBUGMSG ("ipci165_reset: hardware reset\n");
1251
1252   /* reset the HW */
1253   ucr = can_readb(crm_addr + CRM_UCR + 3);
1254   can_writeb(ucr | 0x40, crm_addr + CRM_UCR + 3);
1255   udelay(100);
1256   can_writeb(ucr & ~0x40, crm_addr + CRM_UCR + 3);
1257
1258   /* wait a little bit */
1259   ipci165_delay(200);
1260
1261   /* download FW */
1262   if (ipci165_download_fw(candev)) return -ENODEV;
1263
1264   /* enable irq on HW */
1265   if (ipci165_connect_irq(candev))
1266     {
1267     CANMSG ("Interrupt routine installation for IRQ %i failed!\n",
1268             candev->sysdevptr.pcidev->irq);
1269     return -ENODEV;
1270     }
1271
1272   /* test BCI interface */
1273   test_data = 0x12345678;
1274   size = sizeof(test_data);
1275   if (bci_command(candev, CMD_TEST, size, (char *)&test_data) ||
1276       bci_response(candev, CMD_TEST, &size, (char *)&test_data) ||
1277       (test_data != ~0x12345678))
1278   {
1279     CANMSG ("BCI test failed! Test pattern is %lx\n", test_data);
1280     return -ENODEV;
1281   }
1282
1283   /* get Firmware identification */
1284   /* send command, fw requests 1 dummy byte */
1285   size = BCI_CMD_MAX_LEN;
1286   if (bci_command(candev, CMD_ID, 1, (char *)&test_data) ||
1287       bci_response(candev, CMD_ID, &size, buffer))
1288   {
1289     CANMSG ("Firmware Identification reading failed!\n");
1290     return -ENODEV;
1291   }
1292   CANMSG ("Firmware: %s\n",buffer);
1293
1294   /* get Firmware version */
1295   /* send command, fw requests 1 dummy byte */
1296   size = BCI_CMD_MAX_LEN;
1297   if (bci_command(candev, CMD_VERSION, 1, (char *)&test_data) ||
1298       bci_response(candev, CMD_VERSION, &size, buffer))
1299   {
1300     CANMSG ("Firmware Version reading failed!\n");
1301     return -ENODEV;
1302   }
1303   CANMSG ("Version: %s\n",buffer);
1304
1305   /* get Board Info */
1306   /* send command, fw requests 1 dummy byte */
1307   size = BOARD_INFO_SIZE;
1308   if (bci_command(candev, CMD_GET_BOARD_INFO, 1, (char *)&test_data) ||
1309       bci_response(candev, CMD_GET_BOARD_INFO, &size, (char *) buffer))
1310   {
1311     CANMSG ("Get Board Info failed!\n");
1312     return -ENODEV;
1313   }
1314
1315   chips = le16_to_cpu(*(unsigned short*)(buffer+OF_BOARD_INFO_CHIPS));
1316   /* shouldn't be, but who knows ... */
1317   if (chips > 2) chips = 2;
1318
1319   CANMSG ("Chips: %i\n",chips);
1320   CANMSG ("Chip 1 Type: %s\n",buffer+OF_BOARD_INFO_CHIP1_TYPE);
1321
1322   /* update board info */
1323   if (chips == 1)
1324   {
1325     /* we have to correct the number in candev and release allocated
1326        structures */
1327     candev->nr_all_chips = chips;
1328     canchip_done(candev->chip[1]);
1329
1330   } else CANMSG ("Chip 2 Type: %s\n",buffer+OF_BOARD_INFO_CHIP2_TYPE); 
1331
1332   /* start kernel threads */
1333   for (i = 0 ; i < chips ; i++)
1334   {
1335     chip = candev->chip[i];
1336     chip_data = (struct ipci165_chip_t *)chip->chip_data;
1337     chip_data->kthread.arg = chip;
1338     start_kthread(ipci165_kthread, &chip_data->kthread);
1339   }
1340
1341   CANMSG ("HW is up and working.\n");
1342   return 0;
1343 }
1344
1345 /**
1346  * ipci165_init_hw_data - Initialize hardware cards
1347  * @candev: Pointer to candevice/board structure
1348  *
1349  * Return Value: The function always returns zero
1350  * File: src/ipci165.c
1351  */
1352 int ipci165_init_hw_data(struct candevice_t *candev)
1353 {
1354   struct pci_dev *pcidev = NULL;
1355   unsigned short SubsystemID;
1356
1357   DEBUGMSG ("ipci165_init_hw_data\n");
1358
1359   /* find iPC-I 165 on PCI bus */
1360   do
1361   {
1362     pcidev = pci_find_device(IPCI165_VENDOR_ID, IPCI165_DEVICE_ID, pcidev);
1363     if(pcidev == NULL) return -ENODEV;
1364
1365     /* check subvendor ID */
1366     pci_read_config_word (pcidev, PCI_SUBSYSTEM_ID, &SubsystemID);
1367     if ((SubsystemID != IPCI165_SUBSYSTEM_ID) &&
1368         (SubsystemID != CP350_SUBSYSTEM_ID))
1369       break;
1370   }
1371   while(can_check_dev_taken(pcidev));
1372
1373   /* enable it */
1374   if (pci_enable_device (pcidev))
1375   {
1376     CANMSG ("Cannot enable PCI device\n");
1377     return -EIO;
1378   }
1379
1380   candev->sysdevptr.pcidev = pcidev;
1381   candev->res_addr=0;
1382   candev->nr_82527_chips=0;
1383   candev->nr_sja1000_chips=0;
1384   /* we do not know yet, whether our HW has one or two chan chips. Let's
1385      prepare configuration for maximal configuration = 2. This will be
1386      corrected later on */
1387   candev->nr_all_chips=2; 
1388   candev->flags |= CANDEV_PROGRAMMABLE_IRQ*0;
1389   /* initialize device spinlock */
1390   can_spin_lock_init(&candev->device_lock);
1391
1392   return 0;
1393 }
1394
1395 #define CHIP_TYPE "ipci165"
1396
1397 /**
1398  * ipci165_init_chip_data - Initialize chips
1399  * @candev: Pointer to candevice/board structure)
1400  * @chipnr: Number of the CAN chip on the hardware card
1401  *
1402  * Return Value: The function always returns zero
1403  * File: src/ipci165.c
1404  */
1405 int ipci165_init_chip_data(struct candevice_t *candev, int chipnr)
1406 {
1407   struct canchip_t      *chip = candev->chip[chipnr];
1408   struct ipci165_chip_t *chip_data;
1409
1410   DEBUGMSG ("ipci165_init_chip_data\n");
1411
1412   chip->chip_type = CHIP_TYPE;
1413   chip->chip_base_addr = 0; /* mapping not known yet */
1414   chip->clock = 10000000;
1415   chip->int_clk_reg = 0x0;
1416   chip->int_bus_reg = 0x0;
1417   chip->max_objects = 1;
1418
1419 #if 0
1420   /* initialize interrupt handling only for channel 0. The interrupt
1421      is shared between the channels so we have to work it out in one
1422      interrupt routine. */
1423   if (chipnr == 0)
1424   {
1425     chip->chipspecops->irq_handler=ipci165_irq_handler;
1426     chip->chip_irq=candev->sysdevptr.pcidev->irq;
1427     chip->flags |= CHIP_IRQ_PCI;
1428   } else
1429   {
1430     chip->chipspecops->irq_handler=NULL;
1431   }
1432 #else
1433   chip->chipspecops->irq_handler = NULL;
1434   chip->chip_irq = 0;
1435   chip->flags |= CHIP_IRQ_CUSTOM;
1436 #endif
1437
1438   chip_data = can_checked_malloc(sizeof(struct ipci165_chip_t));
1439   if(!chip_data) return -ENOMEM;
1440   chip_data->rx_queue.idx = 0;
1441   chip_data->rx_queue.addr = 0;
1442   chip_data->tx_queue.idx = 0;
1443   chip_data->tx_queue.addr = 0;
1444   chip->chip_data = chip_data;
1445
1446   CANMSG("initializing ipci165 chip operations\n");
1447   chip->chipspecops->attach_to_chip=ipci165_attach_to_chip;
1448   chip->chipspecops->release_chip=ipci165_release_chip;
1449   chip->chipspecops->chip_config=ipci165_chip_config;
1450   chip->chipspecops->baud_rate=ipci165_baud_rate;
1451   chip->chipspecops->set_btregs=ipci165_set_btregs;
1452   chip->chipspecops->start_chip=ipci165_start_chip;
1453   chip->chipspecops->stop_chip=ipci165_stop_chip;
1454   chip->chipspecops->pre_read_config=ipci165_pre_read_config;
1455   chip->chipspecops->wakeup_tx=ipci165_wakeup_tx;
1456   chip->chipspecops->filtch_rq=NULL;
1457   chip->chipspecops->irq_accept=NULL;
1458
1459   chip->chipspecops->standard_mask=NULL;
1460   chip->chipspecops->extended_mask=NULL;
1461   chip->chipspecops->message15_mask=NULL;
1462   chip->chipspecops->clear_objects=NULL;
1463   chip->chipspecops->config_irqs=NULL;
1464   chip->chipspecops->pre_write_config=NULL;
1465   chip->chipspecops->send_msg=NULL;
1466   chip->chipspecops->check_tx_stat=NULL;
1467   chip->chipspecops->remote_request=NULL;
1468   chip->chipspecops->enable_configuration=NULL;
1469   chip->chipspecops->disable_configuration=NULL;
1470
1471   return 0;
1472 }
1473
1474 /**
1475  * ipci165_init_obj_data - Initialize message buffers
1476  * @chip: Pointer to chip specific structure
1477  * @objnr: Number of the message buffer
1478  *
1479  * Return Value: The function always returns zero
1480  * File: src/ipci165.c
1481  */
1482 int ipci165_init_obj_data(struct canchip_t *chip, int objnr)
1483 {
1484   struct msgobj_t *obj=chip->msgobj[objnr];
1485   
1486   DEBUGMSG ("ipci165_init_obj_data\n");
1487   
1488   obj->obj_base_addr = 0; /* not known yet */
1489   obj->tx_timeout.function = ipci165_do_tx_timeout;
1490   obj->tx_timeout.data = (unsigned long)obj;
1491   return 0;
1492 }
1493
1494 /**
1495  * ipci165_program_irq - Program interrupts
1496  * @candev: Pointer to candevice/board structure
1497  *
1498  * Return value: The function returns zero on success or %-ENODEV on failure
1499  * File: src/ipci165.c
1500  */
1501 int ipci165_program_irq(struct candevice_t *candev)
1502 {
1503   return 0;
1504 }
1505
1506 /**
1507  * ipci165_register - Register Board Support Functions
1508  * @candev: Pointer to hardware/board specific functions
1509  *
1510  * Return value: The function returns zero on success or %-ENODEV on failure
1511  * File: src/ipci165.c
1512  */
1513 int ipci165_register(struct hwspecops_t *hwspecops)
1514 {
1515   hwspecops->request_io = ipci165_request_io;
1516   hwspecops->release_io = ipci165_release_io;
1517   hwspecops->reset = ipci165_reset;
1518   hwspecops->init_hw_data = ipci165_init_hw_data;
1519   hwspecops->init_chip_data = ipci165_init_chip_data;
1520   hwspecops->init_obj_data = ipci165_init_obj_data;
1521   hwspecops->write_register = NULL;
1522   hwspecops->read_register = NULL;
1523   hwspecops->program_irq = ipci165_program_irq;
1524   return 0;
1525 }
1526
1527 #ifdef CAN_DEBUG
1528 void dump_mem(char *ptr, int size)
1529 {
1530   int to, j;
1531   unsigned char str[80], buf[16];
1532   char *strp;
1533
1534   for (; size > 0; size -= 16)
1535   {
1536     to = size > 16 ? 16 : size;
1537     memcpy (buf,ptr, to);
1538     strp = str;
1539     for (j = 0; j < to ; j++)
1540       strp += sprintf(strp, "%02x ",buf[j]);
1541     for (; j < 16 ; j++) 
1542       strp += sprintf(strp, "   ");
1543     for (j = 0; j < to ; j++)
1544       *strp++= isprint(buf[j]) ? buf[j] : '.';
1545
1546     DEBUGMSG ("[%lx] %s\n", (long unsigned)ptr, str);
1547     ptr += to;
1548   }
1549 }
1550 #endif