]> rtime.felk.cvut.cz Git - lincan.git/blob - lincan/src/nsi.c
3fefebc9660dfbdbe73ee1e8d8dc58ebf89b8a0f
[lincan.git] / lincan / src / nsi.c
1 /* nsi.c
2  * Linux CAN-bus device driver.
3  * Written by Arnaud Westenberg email:arnaud@wanadoo.nl
4  * This software is released under the GPL-License.
5  * Version 0.7  6 Aug 2001
6  */ 
7
8 #include <linux/autoconf.h>
9 #if defined (CONFIG_MODVERSIONS) && !defined (MODVERSIONS)
10 #define MODVERSIONS
11 #endif
12
13 #if defined (MODVERSIONS)
14 #include <linux/modversions.h>
15 #endif
16
17 #include <linux/ioport.h>
18 #include <linux/delay.h>
19 #include <asm/errno.h>
20 #include <asm/io.h>
21 #include <asm/irq.h>
22
23 #include "../include/main.h"
24 #include "../include/nsi.h"
25 #include "../include/i82527.h"
26
27 int nsican_irq=-1;
28 unsigned long nsican_base=0x0;
29
30 /* IO_RANGE is the io-memory range that gets reserved, please adjust according
31  * your hardware. Example: #define IO_RANGE 0x100 for i82527 chips or
32  * #define IO_RANGE 0x20 for sja1000 chips.
33  */
34 #define IO_RANGE 0x04
35
36 /* The function template_request_io is used to reserve the io-memory. If your
37  * hardware uses a dedicated memory range as hardware control registers you
38  * will have to add the code to reserve this memory as well.
39  * The reserved memory starts at io_addr, wich is the module parameter io.
40  */
41 int nsi_request_io(unsigned long io_addr)
42 {
43
44         if (check_region(io_addr,IO_RANGE)) {
45                 CANMSG("Unable to open port: 0x%lx\n",io_addr);
46                 return -ENODEV;
47         }
48         else {
49                 request_region(io_addr,IO_RANGE,DEVICE_NAME);
50                 DEBUGMSG("Registered IO-memory: 0x%lx - 0x%lx\n", io_addr, 
51                          io_addr + IO_RANGE - 1);
52         }
53         return 0;
54 }
55
56 /* The function template_release_io is used to free the previously reserved 
57  * io-memory. In case you reserved more memory, don't forget to free it here.
58  */
59 int nsi_release_io(unsigned long io_addr)
60 {
61
62         release_region(io_addr,IO_RANGE);
63
64         return 0;
65 }
66
67 /* The function template_reset is used to give a hardware reset. This is rather
68  * hardware specific so I haven't included example code. Don't forget to check
69  * the reset status of the chip before returning.
70  */
71 int nsi_reset(int card)
72 {
73     int i; 
74
75     DEBUGMSG("Resetting nsi hardware ...\n");
76     /* we don't use template_write_register because we don't use the two first
77        register of the card but the third in order to make a hard reset */
78     outb (1, nsican_base + candevices_p[card]->res_addr);
79     outb (0, nsican_base + candevices_p[card]->res_addr);
80     for (i = 1; i < 1000; i++)
81         udelay (1000);
82     
83     
84     /* Check hardware reset status */ 
85     i=0;
86     while ( (nsi_read_register(nsican_base+iCPU) & iCPU_RST) && (i<=15)) {
87         udelay(20000);
88         i++;
89     }
90     if (i>=15) {
91         CANMSG("Reset status timeout!\n");
92         CANMSG("Please check your hardware.\n");
93         return -ENODEV;
94     }
95     else
96         DEBUGMSG("Chip0 reset status ok.\n");
97
98     return 0;
99 }
100
101 /* The function template_init_hw_data is used to initialize the hardware
102  * structure containing information about the installed CAN-board.
103  * RESET_ADDR represents the io-address of the hardware reset register.
104  * NR_82527 represents the number of intel 82527 chips on the board.
105  * NR_SJA1000 represents the number of philips sja1000 chips on the board.
106  * The flags entry can currently only be PROGRAMMABLE_IRQ to indicate that
107  * the hardware uses programmable interrupts.
108  */
109 #define RESET_ADDR 0x02
110 #define NR_82527 1
111 #define NR_SJA1000 0
112
113 int nsi_init_hw_data(int card) 
114      {
115         candevices_p[card]->res_addr=RESET_ADDR;
116         candevices_p[card]->nr_82527_chips=1;
117         candevices_p[card]->nr_sja1000_chips=0;
118         candevices_p[card]->flags |= PROGRAMMABLE_IRQ;
119
120         return 0;
121 }
122
123 /* The function template_init_chip_data is used to initialize the hardware
124  * structure containing information about the CAN chips.
125  * CHIP_TYPE represents the type of CAN chip. CHIP_TYPE can be "i82527" or
126  * "sja1000".
127  * The chip_base_addr entry represents the start of the 'official' memory map
128  * of the installed chip. It's likely that this is the same as the io_addr
129  * argument supplied at module loading time.
130  * The clock argument holds the chip clock value in Hz.
131  */
132 #define CHIP_TYPE "i82527"
133
134 int nsi_init_chip_data(int card, int chipnr)
135 {
136         candevices_p[card]->chip[chipnr]->chip_type=CHIP_TYPE;
137         candevices_p[card]->chip[chipnr]->chip_base_addr=
138             candevices_p[card]->io_addr;
139         candevices_p[card]->chip[chipnr]->clock = 16000000;
140         nsican_irq=candevices_p[card]->chip[chipnr]->chip_irq;  
141         nsican_base=candevices_p[card]->chip[chipnr]->chip_base_addr;
142         candevices_p[card]->chip[chipnr]->int_cpu_reg = iCPU_DSC;
143         candevices_p[card]->chip[chipnr]->int_clk_reg = iCLK_SL1;
144         candevices_p[card]->chip[chipnr]->int_bus_reg = iBUS_CBY;
145
146         return 0;
147 }
148
149  /* The function template_init_obj_data is used to initialize the hardware
150  * structure containing information about the different message objects on the
151  * CAN chip. In case of the sja1000 there's only one message object but on the
152  * i82527 chip there are 15.
153  * The code below is for a i82527 chip and initializes the object base addresses
154  * The entry obj_base_addr represents the first memory address of the message 
155  * object. In case of the sja1000 obj_base_addr is taken the same as the chips
156  * base address.
157  * Unless the hardware uses a segmented memory map, flags can be set zero.
158  */
159 int nsi_init_obj_data(int chipnr, int objnr)
160 {
161
162         chips_p[chipnr]->msgobj[objnr]->obj_base_addr=
163             chips_p[chipnr]->chip_base_addr+(objnr+1)*0x10;
164         chips_p[chipnr]->msgobj[objnr]->flags=0;
165         
166         return 0;
167 }
168
169 /* The function template_program_irq is used for hardware that uses programmable
170  * interrupts. If your hardware doesn't use programmable interrupts you should
171  * not set the candevices_t->flags entry to PROGRAMMABLE_IRQ and leave this
172  * function unedited. Again this function is hardware specific so there's no
173  * example code.
174  */
175 int nsi_program_irq(int card)
176 {
177         return 0;
178 }
179
180 /* The function template_write_register is used to write to hardware registers
181  * on the CAN chip. You should only have to edit this function if your hardware
182  * uses some specific write process.
183  */
184 void nsi_write_register(unsigned char data, unsigned long address)
185 {
186     /* address is an absolute address */
187
188     /* the nsi card has two registers, the address register at 0x0
189        and the data register at 0x01 */
190
191     /* write the relative address on the eight LSB bits 
192        and the data on the eight MSB bits in one time */
193     outw(address-nsican_base + (256 * data), nsican_base); 
194 }
195
196 /* The function template_read_register is used to read from hardware registers
197  * on the CAN chip. You should only have to edit this function if your hardware
198  * uses some specific read process.
199  */
200 unsigned nsi_read_register(unsigned long address)
201 {
202     /* this is the same thing that the function write_register.
203        We use the two register, we write the address where we 
204        want to read in a first time. In a second time we read the
205        data */
206     unsigned char ret;
207     
208     disable_irq(nsican_irq); 
209     outb(address-nsican_base, nsican_base);
210     ret=inb(nsican_base+1);
211     enable_irq(nsican_irq); 
212     return ret;
213 }
214
215
216  /* !!! Don't change this function !!! */
217 int nsi_register(struct hwspecops_t *hwspecops)
218 {
219         hwspecops->request_io = nsi_request_io;
220         hwspecops->release_io = nsi_release_io;
221         hwspecops->reset = nsi_reset;
222         hwspecops->init_hw_data = nsi_init_hw_data;
223         hwspecops->init_chip_data = nsi_init_chip_data;
224         hwspecops->init_obj_data = nsi_init_obj_data;
225         hwspecops->write_register = nsi_write_register;
226         hwspecops->read_register = nsi_read_register;
227         hwspecops->program_irq = nsi_program_irq;
228         return 0;
229 }