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