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