]> rtime.felk.cvut.cz Git - lincan.git/blob - lincan/src/ssv.c
The first enhanced version of Linux CAN-bus driver for OCERA project
[lincan.git] / lincan / src / ssv.c
1 /* ssv.c
2  * Linux CAN-bus device driver.
3  * Written by Arnaud Westenberg email:arnaud@casema.net
4  * This software is released under the GPL-License.
5  * Version 0.6  18 Sept 2000
6  */ 
7
8 #include <linux/autoconf.h>
9 #if defined (CONFIG_MODVERSIONS) && !defined (MODVERSIONS)
10 #define MODVERSIONS
11 #endif
12
13 #ifdef 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/ssv.h"
25 #include "../include/i82527.h"
26
27 int ssvcan_irq[2]={-1,-1};
28 unsigned long ssvcan_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 ssv_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 ssv_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 ssv_reset(int card)
72 {
73     int i; 
74
75     DEBUGMSG("Resetting ssv hardware ...\n");
76     ssv_write_register(1,ssvcan_base+iCPU);
77     ssv_write_register(0,ssvcan_base+iCPU);
78     ssv_write_register(1,ssvcan_base+0x100+iCPU);
79     ssv_write_register(0,ssvcan_base+0x100+iCPU);
80
81     for (i = 1; i < 1000; i++)
82         udelay (1000);
83
84     /* Check hardware reset status */ 
85     i=0;
86     while ( (ssv_read_register(ssvcan_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     /* Check hardware reset status */ 
99     i=0;
100     while ( (ssv_read_register(ssvcan_base+0x100+iCPU) & iCPU_RST) && (i<=15)) {
101         udelay(20000);
102         i++;
103     }
104     if (i>=15) {
105         CANMSG("Reset status timeout!\n");
106         CANMSG("Please check your hardware.\n");
107         return -ENODEV;
108     }
109     else
110         DEBUGMSG("Chip1 reset status ok.\n");
111
112
113
114     return 0;
115 }
116
117 /* The function template_init_hw_data is used to initialize the hardware
118  * structure containing information about the installed CAN-board.
119  * RESET_ADDR represents the io-address of the hardware reset register.
120  * NR_82527 represents the number of intel 82527 chips on the board.
121  * NR_SJA1000 represents the number of philips sja1000 chips on the board.
122  * The flags entry can currently only be PROGRAMMABLE_IRQ to indicate that
123  * the hardware uses programmable interrupts.
124  */
125 #define RESET_ADDR 0x02
126 #define NR_82527 2
127 #define NR_SJA1000 0
128
129 int ssv_init_hw_data(int card) 
130 {
131     candevices_p[card]->res_addr=RESET_ADDR;
132     candevices_p[card]->nr_82527_chips=NR_82527;
133     candevices_p[card]->nr_sja1000_chips=0;
134     candevices_p[card]->flags |= PROGRAMMABLE_IRQ;
135
136     return 0;
137 }
138
139 /* The function template_init_chip_data is used to initialize the hardware
140  * structure containing information about the CAN chips.
141  * CHIP_TYPE represents the type of CAN chip. CHIP_TYPE can be "i82527" or
142  * "sja1000".
143  * The chip_base_addr entry represents the start of the 'official' memory map
144  * of the installed chip. It's likely that this is the same as the io_addr
145  * argument supplied at module loading time.
146  * The clock argument holds the chip clock value in Hz.
147  */
148 #define CHIP_TYPE "i82527"
149
150 int ssv_init_chip_data(int card, int chipnr)
151 {
152     candevices_p[card]->chip[chipnr]->chip_type=CHIP_TYPE;
153     candevices_p[card]->chip[chipnr]->chip_base_addr=
154         candevices_p[card]->io_addr+0x100*chipnr;
155     candevices_p[card]->chip[chipnr]->clock = 16000000;
156     ssvcan_irq[chipnr]=candevices_p[card]->chip[chipnr]->chip_irq;
157
158     ssvcan_base=candevices_p[card]->io_addr;
159
160     candevices_p[card]->chip[chipnr]->int_cpu_reg = iCPU_DSC;
161     candevices_p[card]->chip[chipnr]->int_clk_reg = iCLK_SL1;
162     candevices_p[card]->chip[chipnr]->int_bus_reg = iBUS_CBY;
163     return 0;
164 }
165
166  /* The function template_init_obj_data is used to initialize the hardware
167  * structure containing information about the different message objects on the
168  * CAN chip. In case of the sja1000 there's only one message object but on the
169  * i82527 chip there are 15.
170  * The code below is for a i82527 chip and initializes the object base addresses
171  * The entry obj_base_addr represents the first memory address of the message 
172  * object. In case of the sja1000 obj_base_addr is taken the same as the chips
173  * base address.
174  * Unless the hardware uses a segmented memory map, flags can be set zero.
175  */
176 int ssv_init_obj_data(int chipnr, int objnr)
177 {
178
179     chips_p[chipnr]->msgobj[objnr]->obj_base_addr=
180         chips_p[chipnr]->chip_base_addr+(objnr+1)*0x10;
181     chips_p[chipnr]->msgobj[objnr]->flags=0;
182         
183     return 0;
184 }
185
186 /* The function template_program_irq is used for hardware that uses programmable
187  * interrupts. If your hardware doesn't use programmable interrupts you should
188  * not set the candevices_t->flags entry to PROGRAMMABLE_IRQ and leave this
189  * function unedited. Again this function is hardware specific so there's no
190  * example code.
191  */
192 int ssv_program_irq(int card)
193 {
194     return 0;
195 }
196
197 /* The function template_write_register is used to write to hardware registers
198  * on the CAN chip. You should only have to edit this function if your hardware
199  * uses some specific write process.
200  */
201 void ssv_write_register(unsigned char data, unsigned long address)
202 {
203     /* address is an absolute address */
204
205     /* the ssv card has two registers, the address register at 0x0
206        and the data register at 0x01 */
207
208     /* write the relative address on the eight LSB bits 
209      and the data on the eight MSB bits in one time */
210     if((address-ssvcan_base)<0x100)
211         outw(address-ssvcan_base + (256 * data), ssvcan_base);
212     else
213         outw(address-ssvcan_base-0x100 + (256 * data), ssvcan_base+0x02);
214 }
215
216 /* The function template_read_register is used to read from hardware registers
217  * on the CAN chip. You should only have to edit this function if your hardware
218  * uses some specific read process.
219  */
220 unsigned ssv_read_register(unsigned long address)
221 {
222     /* this is the same thing that the function write_register.
223        We use the two register, we write the address where we 
224        want to read in a first time. In a second time we read the
225        data */
226     unsigned char ret;
227     
228
229     if((address-ssvcan_base)<0x100)
230     {
231         disable_irq(ssvcan_irq[0]);
232         outb(address-ssvcan_base, ssvcan_base);
233         ret=inb(ssvcan_base+1);
234         enable_irq(ssvcan_irq[0]);
235     }
236     else
237     {
238         disable_irq(ssvcan_irq[1]);
239         outb(address-ssvcan_base-0x100, ssvcan_base+0x02);
240         ret=inb(ssvcan_base+1+0x02);
241         enable_irq(ssvcan_irq[1]);
242     }
243
244     return ret;
245 }
246
247
248  /* !!! Don't change this function !!! */
249 int ssv_register(struct hwspecops_t *hwspecops)
250 {
251     hwspecops->request_io = ssv_request_io;
252     hwspecops->release_io = ssv_release_io;
253     hwspecops->reset = ssv_reset;
254     hwspecops->init_hw_data = ssv_init_hw_data;
255     hwspecops->init_chip_data = ssv_init_chip_data;
256     hwspecops->init_obj_data = ssv_init_obj_data;
257     hwspecops->write_register = ssv_write_register;
258     hwspecops->read_register = ssv_read_register;
259     hwspecops->program_irq = ssv_program_irq;
260     return 0;
261 }