]> rtime.felk.cvut.cz Git - lincan.git/blobdiff - lincan/src/sja1000p.c
SJA1000 driver interrupts processing modified to not rely on interrupt register for...
[lincan.git] / lincan / src / sja1000p.c
index 39dff690ddebe684fdc5abd999bdb254dcc5c2eb..59fa68dcb39ed4a807ca054fd9a04445c2f165a8 100644 (file)
@@ -18,7 +18,7 @@
  * sja1000p_enable_configuration - enable chip configuration mode
  * @chip: pointer to chip state structure
  */
-int sja1000p_enable_configuration(struct chip_t *chip)
+int sja1000p_enable_configuration(struct canchip_t *chip)
 {
        int i=0;
        enum sja1000_PeliCAN_MOD flags;
@@ -48,7 +48,7 @@ int sja1000p_enable_configuration(struct chip_t *chip)
  * sja1000p_disable_configuration - disable chip configuration mode
  * @chip: pointer to chip state structure
  */
-int sja1000p_disable_configuration(struct chip_t *chip)
+int sja1000p_disable_configuration(struct canchip_t *chip)
 {
        int i=0;
        enum sja1000_PeliCAN_MOD flags;
@@ -86,7 +86,7 @@ int sja1000p_disable_configuration(struct chip_t *chip)
  * Return Value: negative value reports error.
  * File: src/sja1000p.c
  */
-int sja1000p_chip_config(struct chip_t *chip)
+int sja1000p_chip_config(struct canchip_t *chip)
 {
        int i;
        unsigned char n, r;
@@ -96,6 +96,10 @@ int sja1000p_chip_config(struct chip_t *chip)
 
        /* Set mode, clock out, comparator */
        can_write_reg(chip,sjaCDR_PELICAN|chip->sja_cdr_reg,SJACDR); 
+
+       /* Ensure, that interrupts are disabled even on the chip level now */
+       can_write_reg(chip, sjaDISABLE_INTERRUPTS, SJAIER);
+
        /* Set driver output configuration */
        can_write_reg(chip,chip->sja_ocr_reg,SJAOCR); 
        
@@ -138,7 +142,7 @@ int sja1000p_chip_config(struct chip_t *chip)
  * Return Value: negative value reports error.
  * File: src/sja1000p.c
  */
-int sja1000p_extended_mask(struct chip_t *chip, unsigned long code, unsigned  long mask)
+int sja1000p_extended_mask(struct canchip_t *chip, unsigned long code, unsigned  long mask)
 {
        int i;
 
@@ -173,7 +177,7 @@ int sja1000p_extended_mask(struct chip_t *chip, unsigned long code, unsigned  lo
  * Return Value: negative value reports error.
  * File: src/sja1000p.c
  */
-int sja1000p_baud_rate(struct chip_t *chip, int rate, int clock, int sjw,
+int sja1000p_baud_rate(struct canchip_t *chip, int rate, int clock, int sjw,
                                                        int sampl_pt, int flags)
 {
        int best_error = 1000000000, error;
@@ -240,7 +244,7 @@ int sja1000p_baud_rate(struct chip_t *chip, int rate, int clock, int sjw,
  *
  * File: src/sja1000p.c
  */
-void sja1000p_read(struct chip_t *chip, struct msgobj_t *obj) {
+void sja1000p_read(struct canchip_t *chip, struct msgobj_t *obj) {
        int i, flags, len, datastart;
        do {
                flags = can_read_reg(chip,SJAFRM);
@@ -286,7 +290,7 @@ void sja1000p_read(struct chip_t *chip, struct msgobj_t *obj) {
  *     Positive value indicates immediate reception of message.
  * File: src/sja1000p.c
  */
-int sja1000p_pre_read_config(struct chip_t *chip, struct msgobj_t *obj)
+int sja1000p_pre_read_config(struct canchip_t *chip, struct msgobj_t *obj)
 {
        int status;
        status=can_read_reg(chip,SJASR);
@@ -325,7 +329,7 @@ int sja1000p_pre_read_config(struct chip_t *chip, struct msgobj_t *obj)
  * Return Value: negative value reports error.
  * File: src/sja1000p.c
  */
-int sja1000p_pre_write_config(struct chip_t *chip, struct msgobj_t *obj, 
+int sja1000p_pre_write_config(struct canchip_t *chip, struct msgobj_t *obj, 
                                                        struct canmsg_t *msg)
 {
        int i=0; 
@@ -404,7 +408,7 @@ int sja1000p_pre_write_config(struct chip_t *chip, struct msgobj_t *obj,
  * Return Value: negative value reports error.
  * File: src/sja1000p.c
  */
-int sja1000p_send_msg(struct chip_t *chip, struct msgobj_t *obj, 
+int sja1000p_send_msg(struct canchip_t *chip, struct msgobj_t *obj, 
                                                        struct canmsg_t *msg)
 {
        can_write_reg(chip, sjaCMR_TR, SJACMR);
@@ -421,7 +425,7 @@ int sja1000p_send_msg(struct chip_t *chip, struct msgobj_t *obj,
  *     Zero value indicates finishing of all issued transmission requests.
  * File: src/sja1000p.c
  */
-int sja1000p_check_tx_stat(struct chip_t *chip)
+int sja1000p_check_tx_stat(struct canchip_t *chip)
 {
        if (can_read_reg(chip,SJASR) & sjaSR_TCS)
                return 0;
@@ -438,7 +442,7 @@ int sja1000p_check_tx_stat(struct chip_t *chip)
  * Return Value: negative value reports error.
  * File: src/sja1000p.c
  */
-int sja1000p_set_btregs(struct chip_t *chip, unsigned short btr0, 
+int sja1000p_set_btregs(struct canchip_t *chip, unsigned short btr0, 
                                                        unsigned short btr1)
 {
        if (sja1000p_enable_configuration(chip))
@@ -459,7 +463,7 @@ int sja1000p_set_btregs(struct chip_t *chip, unsigned short btr0,
  * Return Value: negative value reports error.
  * File: src/sja1000p.c
  */
-int sja1000p_start_chip(struct chip_t *chip)
+int sja1000p_start_chip(struct canchip_t *chip)
 {
        enum sja1000_PeliCAN_MOD flags;
 
@@ -476,7 +480,7 @@ int sja1000p_start_chip(struct chip_t *chip)
  * Return Value: negative value reports error.
  * File: src/sja1000p.c
  */
-int sja1000p_stop_chip(struct chip_t *chip)
+int sja1000p_stop_chip(struct canchip_t *chip)
 {
        enum sja1000_PeliCAN_MOD flags;
 
@@ -486,6 +490,32 @@ int sja1000p_stop_chip(struct chip_t *chip)
        return 0;
 }
 
+/**
+ * sja1000p_attach_to_chip: - attaches to the chip, setups registers and state
+ * @chip: pointer to chip state structure
+ *
+ * Return Value: negative value reports error.
+ * File: src/sja1000p.c
+ */
+int sja1000p_attach_to_chip(struct canchip_t *chip)
+{
+       return 0;
+}
+
+/**
+ * sja1000p_release_chip: - called before chip structure removal if %CHIP_ATTACHED is set
+ * @chip: pointer to chip state structure
+ *
+ * Return Value: negative value reports error.
+ * File: src/sja1000p.c
+ */
+int sja1000p_release_chip(struct canchip_t *chip)
+{
+       sja1000p_stop_chip(chip);
+       can_write_reg(chip, sjaDISABLE_INTERRUPTS, SJAIER);
+
+       return 0;
+}
 
 /**
  * sja1000p_remote_request: - configures message object and asks for RTR message
@@ -495,7 +525,7 @@ int sja1000p_stop_chip(struct chip_t *chip)
  * Return Value: negative value reports error.
  * File: src/sja1000p.c
  */
-int sja1000p_remote_request(struct chip_t *chip, struct msgobj_t *obj)
+int sja1000p_remote_request(struct canchip_t *chip, struct msgobj_t *obj)
 {
        CANMSG("sja1000p_remote_request not implemented\n");
        return -ENOSYS;
@@ -510,7 +540,7 @@ int sja1000p_remote_request(struct chip_t *chip, struct msgobj_t *obj)
  * Return Value: negative value reports error.
  * File: src/sja1000p.c
  */
-int sja1000p_standard_mask(struct chip_t *chip, unsigned short code,
+int sja1000p_standard_mask(struct canchip_t *chip, unsigned short code,
                unsigned short mask)
 {
        CANMSG("sja1000p_standard_mask not implemented\n");
@@ -524,7 +554,7 @@ int sja1000p_standard_mask(struct chip_t *chip, unsigned short code,
  * Return Value: negative value reports error.
  * File: src/sja1000p.c
  */
-int sja1000p_clear_objects(struct chip_t *chip)
+int sja1000p_clear_objects(struct canchip_t *chip)
 {
        CANMSG("sja1000p_clear_objects not implemented\n");
        return -ENOSYS;
@@ -538,7 +568,7 @@ int sja1000p_clear_objects(struct chip_t *chip)
  * Return Value: negative value reports error.
  * File: src/sja1000p.c
  */
-int sja1000p_config_irqs(struct chip_t *chip, short irqs)
+int sja1000p_config_irqs(struct canchip_t *chip, short irqs)
 {
        CANMSG("sja1000p_config_irqs not implemented\n");
        return -ENOSYS;
@@ -555,7 +585,7 @@ int sja1000p_config_irqs(struct chip_t *chip, short irqs)
  * sja1000p_irq_write_handler() for transmit events.
  * File: src/sja1000p.c
  */
-void sja1000p_irq_write_handler(struct chip_t *chip, struct msgobj_t *obj)
+void sja1000p_irq_write_handler(struct canchip_t *chip, struct msgobj_t *obj)
 {
        int cmd;
        
@@ -573,9 +603,11 @@ void sja1000p_irq_write_handler(struct chip_t *chip, struct msgobj_t *obj)
                obj->tx_slot=NULL;
        }
        
+       can_msgobj_clear_fl(obj,TX_PENDING);
        cmd=canque_test_outslot(obj->qends, &obj->tx_qedge, &obj->tx_slot);
        if(cmd<0)
                return;
+       can_msgobj_set_fl(obj,TX_PENDING);
 
        if (chip->chipspecops->pre_write_config(chip, obj, &obj->tx_slot->msg)) {
                obj->ret = -1;
@@ -599,10 +631,7 @@ void sja1000p_irq_write_handler(struct chip_t *chip, struct msgobj_t *obj)
 /**
  * sja1000p_irq_handler: - interrupt service routine
  * @irq: interrupt vector number, this value is system specific
- * @dev_id: driver private pointer registered at time of request_irq() call.
- *     The CAN driver uses this pointer to store relationship of interrupt
- *     to chip state structure - @struct chip_t
- * @regs: system dependent value pointing to registers stored in exception frame
+ * @chip: pointer to chip state structure
  * 
  * Interrupt handler is activated when state of CAN controller chip changes,
  * there is message to be read or there is more space for new messages or
@@ -611,10 +640,11 @@ void sja1000p_irq_write_handler(struct chip_t *chip, struct msgobj_t *obj)
  * message queues.
  * File: src/sja1000p.c
  */
-int sja1000p_irq_handler(int irq, struct chip_t *chip)
+int sja1000p_irq_handler(int irq, struct canchip_t *chip)
 {
        int irq_register, status, error_code;
        struct msgobj_t *obj=chip->msgobj[0];
+       int loop_cnt=CHIP_MAX_IRQLOOP;
 
        irq_register=can_read_reg(chip,SJAIR);
 //     DEBUGMSG("sja1000_irq_handler: SJAIR:%02x\n",irq_register);
@@ -629,58 +659,79 @@ int sja1000p_irq_handler(int irq, struct chip_t *chip)
                return CANCHIP_IRQ_NONE;
        }
 
-       if ((irq_register & sjaIR_RI) != 0) {
-               DEBUGMSG("sja1000_irq_handler: RI\n");
-               sja1000p_read(chip,obj);
-               obj->ret = 0;
-       }
-       if ((irq_register & sjaIR_TI) != 0) {
-               DEBUGMSG("sja1000_irq_handler: TI\n");
-               obj->ret = 0;
-               can_msgobj_set_fl(obj,TX_REQUEST);
-               while(!can_msgobj_test_and_set_fl(obj,TX_LOCK)){
-                       can_msgobj_clear_fl(obj,TX_REQUEST);
-
-                       if (can_read_reg(chip, SJASR) & sjaSR_TBS)
-                               sja1000p_irq_write_handler(chip, obj);
-
-                       can_msgobj_clear_fl(obj,TX_LOCK);
-                       if(!can_msgobj_test_fl(obj,TX_REQUEST)) break;
-                       DEBUGMSG("TX looping in sja1000_irq_handler\n");
+       status=can_read_reg(chip,SJASR);
+
+       do {
+
+               if(!loop_cnt--) {
+                       CANMSG("sja1000p_irq_handler IRQ %d stuck\n",irq);
+                       return CANCHIP_IRQ_STUCK;
                }
-       }
-       if ((irq_register & (sjaIR_EI|sjaIR_BEI|sjaIR_EPI|sjaIR_DOI)) != 0) { 
-               // Some error happened
-               status=can_read_reg(chip,SJASR);
-               error_code=can_read_reg(chip,SJAECC);
-               CANMSG("Error: status register: 0x%x irq_register: 0x%02x error: 0x%02x\n",
-                       status, irq_register, error_code);
+
+               /* (irq_register & sjaIR_RI) */
+               /*      old variant using SJAIR, collides with intended use with irq_accept */
+               if (status & sjaSR_RBS) {
+                       DEBUGMSG("sja1000_irq_handler: RI or RBS\n");
+                       sja1000p_read(chip,obj);
+                       obj->ret = 0;
+               }
+
+               /* (irq_register & sjaIR_TI) */
+               /*      old variant using SJAIR, collides with intended use with irq_accept */
+               if ((status & sjaSR_TBS) && can_msgobj_test_fl(obj,TX_PENDING)) {
+                       DEBUGMSG("sja1000_irq_handler: TI or TX_PENDING and TBS\n");
+                       obj->ret = 0;
+                       can_msgobj_set_fl(obj,TX_REQUEST);
+                       while(!can_msgobj_test_and_set_fl(obj,TX_LOCK)){
+                               can_msgobj_clear_fl(obj,TX_REQUEST);
+
+                               if (can_read_reg(chip, SJASR) & sjaSR_TBS)
+                                       sja1000p_irq_write_handler(chip, obj);
+
+                               can_msgobj_clear_fl(obj,TX_LOCK);
+                               if(!can_msgobj_test_fl(obj,TX_REQUEST)) break;
+                               DEBUGMSG("TX looping in sja1000_irq_handler\n");
+                       }
+               }
+               if ((irq_register & (sjaIR_EI|sjaIR_BEI|sjaIR_EPI|sjaIR_DOI)) != 0) { 
+                       // Some error happened
+                       error_code=can_read_reg(chip,SJAECC);
+                       CANMSG("Error: status register: 0x%x irq_register: 0x%02x error: 0x%02x\n",
+                               status, irq_register, error_code);
 // FIXME: chip should be brought to usable state. Transmission cancelled if in progress.
 // Reset flag set to 0 if chip is already off the bus. Full state report
                obj->ret=-1;
                
-               if(error_code == 0xd9) {
-                       obj->ret= -ENXIO;
-                       /* no such device or address - no ACK received */
-               }
-               if(obj->tx_retry_cnt++>MAX_RETR) {
-                       can_write_reg(chip, sjaCMR_AT, SJACMR); // cancel any transmition
-                       obj->tx_retry_cnt = 0;
-               }
-               if(status&sjaSR_BS) {
-                       CANMSG("bus-off, resetting sja1000p\n");
-                       can_write_reg(chip, 0, SJAMOD);
-               }
-               
-               if(obj->tx_slot){
-                       canque_notify_inends(obj->tx_qedge, CANQUEUE_NOTIFY_ERRTX_BUS);
-                       /*canque_free_outslot(obj->qends, obj->tx_qedge, obj->tx_slot);
-                       obj->tx_slot=NULL;*/
+                       if(error_code == 0xd9) {
+                               obj->ret= -ENXIO;
+                               /* no such device or address - no ACK received */
+                       }
+                       if(obj->tx_retry_cnt++>MAX_RETR) {
+                               can_write_reg(chip, sjaCMR_AT, SJACMR); // cancel any transmition
+                               obj->tx_retry_cnt = 0;
+                       }
+                       if(status&sjaSR_BS) {
+                               CANMSG("bus-off, resetting sja1000p\n");
+                               can_write_reg(chip, 0, SJAMOD);
+                       }
+
+                       if(obj->tx_slot){
+                               canque_notify_inends(obj->tx_qedge, CANQUEUE_NOTIFY_ERRTX_BUS);
+                               /*canque_free_outslot(obj->qends, obj->tx_qedge, obj->tx_slot);
+                               obj->tx_slot=NULL;*/
+                       }
+
+               } else {
+                       obj->tx_retry_cnt=0;
                }
 
-       } else {
-               obj->tx_retry_cnt=0;
-       }
+               irq_register=can_read_reg(chip,SJAIR);
+       
+               status=can_read_reg(chip,SJASR);
+
+       } while((irq_register & (sjaIR_BEI|sjaIR_EPI|sjaIR_DOI|sjaIR_EI|sjaIR_TI|sjaIR_RI)) ||
+               ((status & sjaSR_TBS) && can_msgobj_test_fl(obj,TX_PENDING)) ||
+               (status & sjaSR_RBS));
 
        return CANCHIP_IRQ_HANDLED;
 }
@@ -696,11 +747,12 @@ int sja1000p_irq_handler(int irq, struct chip_t *chip)
  * Return Value: negative value reports error.
  * File: src/sja1000p.c
  */
-int sja1000p_wakeup_tx(struct chip_t *chip, struct msgobj_t *obj)
+int sja1000p_wakeup_tx(struct canchip_t *chip, struct msgobj_t *obj)
 {
        
        can_preempt_disable();
        
+       can_msgobj_set_fl(obj,TX_PENDING);
        can_msgobj_set_fl(obj,TX_REQUEST);
        while(!can_msgobj_test_and_set_fl(obj,TX_LOCK)){
                can_msgobj_clear_fl(obj,TX_REQUEST);
@@ -737,10 +789,13 @@ int sja1000p_register(struct chipspecops_t *chipspecops)
        chipspecops->remote_request=sja1000p_remote_request;
        chipspecops->enable_configuration=sja1000p_enable_configuration;
        chipspecops->disable_configuration=sja1000p_disable_configuration;
+       chipspecops->attach_to_chip=sja1000p_attach_to_chip;
+       chipspecops->release_chip=sja1000p_release_chip;
        chipspecops->set_btregs=sja1000p_set_btregs;
        chipspecops->start_chip=sja1000p_start_chip;
        chipspecops->stop_chip=sja1000p_stop_chip;
        chipspecops->irq_handler=sja1000p_irq_handler;
+       chipspecops->irq_accept=NULL;
        return 0;
 }
 
@@ -752,7 +807,7 @@ int sja1000p_register(struct chipspecops_t *chipspecops)
  *
  * Return Value: returns negative number in the case of fail
  */
-int sja1000p_fill_chipspecops(struct chip_t *chip)
+int sja1000p_fill_chipspecops(struct canchip_t *chip)
 {
        chip->chip_type="sja1000p";
        chip->max_objects=1;