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