]> rtime.felk.cvut.cz Git - lincan.git/blob - lincan/src/pip.c
The first enhanced version of Linux CAN-bus driver for OCERA project
[lincan.git] / lincan / src / pip.c
1 /* pip.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 <linux/string.h>
20 #include <asm/errno.h>
21 #include <asm/io.h>
22
23 #include "../include/main.h"
24 #include "../include/pip.h"
25 #include "../include/i82527.h"
26
27 int pip5_request_io(unsigned long io_addr)
28 {
29         if (io_addr != 0x8000) {
30                 CANMSG("Invalid base io address\n");
31                 CANMSG("The PIP5 uses a fixed base address of 0x8000,\n");
32                 CANMSG("please consult your user manual.\n");
33                 return -ENODEV;
34         }
35         if (check_region(io_addr,0x100)) {
36                 CANMSG("Unable to open port: 0x%lx\n",io_addr);
37                 return -ENODEV;
38         }
39         else if(check_region(io_addr+0x102,0x01)) {
40                 CANMSG("Unable to open port: 0x%lx\n",io_addr+0x102);
41                 return -ENODEV;
42         }
43         else {
44                 request_region(io_addr,0x100,DEVICE_NAME);
45                 DEBUGMSG("Registered IO-memory: 0x%lx - 0x%lx\n", io_addr, io_addr + 0x100 - 1);
46                 request_region(io_addr+0x102,0x01,DEVICE_NAME);
47                 DEBUGMSG("Registered IO-memory: 0x%lx\n", io_addr+0x102);
48         }
49         return 0;
50 }
51
52 int pip6_request_io(unsigned long io_addr)
53 {
54         if ( (io_addr != 0x1000)&&(io_addr != 0x8000)&&(io_addr != 0xe000)) {
55                 CANMSG("Invalid base io address\n");
56                 CANMSG("Valid values for the PIP6 are: 0x1000, 0x8000 or 0xe000\n");
57                 CANMSG("Please consult your user manual.\n");
58                 return -ENODEV;
59         }
60         if (check_region(io_addr,0x100)) {
61                 CANMSG("Unable to open port: 0x%lx\n",io_addr);
62                 return -ENODEV;
63         }
64         else if (check_region(0x804, 0x02)) {
65                 CANMSG("Unable to open port: 0x%x\n", 0x804);
66                 return -ENODEV;
67         }
68         else {
69                 request_region(io_addr,0x100, DEVICE_NAME);
70                 DEBUGMSG("Registered IO-memory: 0x%lx - 0x%lx\n", io_addr, io_addr + 0x100 -1);
71                 request_region(0x804,0x02,DEVICE_NAME);
72                 DEBUGMSG("Registered IO-memory : 0x%x - 0x%x\n",0x804,0x805);
73         }
74         return 0;
75 }
76
77 int pip5_release_io(unsigned long io_addr)
78 {
79         release_region(io_addr,0x100);
80         release_region(io_addr+0x102,0x01);
81
82         return 0;
83 }
84
85 int pip6_release_io(unsigned long io_addr)
86 {
87         release_region(io_addr,0x100);
88         release_region(0x804,0x02);
89
90         return 0;
91 }
92
93 int pip_reset(int card)
94 {
95         int i=0, res_value=0;
96
97         DEBUGMSG("Resetting %s hardware ...\n", candevices_p[card]->hwname);
98         if (!strcmp(candevices_p[card]->hwname,"pip5"))
99                 res_value = 0xcf;
100         else
101                 res_value = 0x01;
102         while (i < 1000000) {
103                 i++;
104                 outb(res_value,candevices_p[card]->res_addr);
105         }
106         outb(0x0,candevices_p[card]->res_addr);
107
108         /* Check hardware reset status */
109         i=0;
110         while ( (inb(candevices_p[card]->io_addr+iCPU) & iCPU_RST) && (i<=15)) {
111                 udelay(20000);
112                 i++;
113         }
114         if (i>=15) {
115                 CANMSG("Reset status timeout!\n");
116                 CANMSG("Please check your hardware.\n");
117                 return -ENODEV;
118         }
119         else
120                 DEBUGMSG("Chip0 reset status ok.\n");
121         
122
123         return 0;
124 }
125
126 int pip_init_hw_data(int card) 
127 {
128         if (!strcmp(candevices_p[card]->hwname,"pip5"))
129                 candevices_p[card]->res_addr=candevices_p[card]->io_addr+0x102;
130         else
131                 candevices_p[card]->res_addr=0x805;
132         candevices_p[card]->nr_82527_chips=1;
133         candevices_p[card]->nr_sja1000_chips=0;
134         candevices_p[card]->flags |= PROGRAMMABLE_IRQ;
135
136         return 0;
137 }
138
139 int pip_init_chip_data(int card, int chipnr)
140 {
141         candevices_p[card]->chip[chipnr]->chip_type="i82527";
142         candevices_p[card]->chip[chipnr]->chip_base_addr=candevices_p[card]->io_addr;
143         candevices_p[card]->chip[chipnr]->clock = 16000000;
144         if (!strcmp(candevices_p[card]->hwname,"pip5"))
145                 candevices_p[card]->chip[chipnr]->int_cpu_reg = iCPU_DSC;
146         else
147                 candevices_p[card]->chip[chipnr]->int_cpu_reg = 0x0;
148         candevices_p[card]->chip[chipnr]->int_clk_reg = iCLK_SL1;
149         candevices_p[card]->chip[chipnr]->int_bus_reg = iBUS_CBY;
150         candevices_p[card]->chip[chipnr]->sja_cdr_reg = 0;
151         candevices_p[card]->chip[chipnr]->sja_ocr_reg = 0;
152
153         return 0;
154 }
155
156 int pip_init_obj_data(int chipnr, int objnr)
157 {
158         chips_p[chipnr]->msgobj[objnr]->obj_base_addr=chips_p[chipnr]->chip_base_addr+(objnr+1)*0x10;
159         chips_p[chipnr]->msgobj[objnr]->flags=0;
160         
161         return 0;
162 }
163
164 int pip5_program_irq(int card)
165 {
166         outb(0x01, candevices_p[card]->res_addr);
167         switch (candevices_p[card]->chip[0]->chip_irq) {
168                 case  3: { outb(0x03, candevices_p[card]->res_addr); break; }
169                 case  4: { outb(0x05, candevices_p[card]->res_addr); break; }
170                 case  5: { outb(0x07, candevices_p[card]->res_addr); break; }
171                 case 10: { outb(0x09, candevices_p[card]->res_addr); break; }
172                 case 11: { outb(0x0c, candevices_p[card]->res_addr); break; }
173                 case 15: { outb(0x0d, candevices_p[card]->res_addr); break; }
174                 default: {
175                 CANMSG("Supplied interrupt is not supported by the hardware\n");
176                 CANMSG("Please consult your user manual.\n");
177                 return -ENODEV;
178                 }
179         }
180         outb(0x00, candevices_p[card]->res_addr);
181
182         return 0;
183 }
184
185 int pip6_program_irq(int card)
186 {
187         unsigned char can_int = 0, can_addr = 0;
188
189         can_int = candevices_p[card]->chip[0]->chip_irq;
190         if ((can_int != 3) && (can_int != 4) && (can_int != 5) && (can_int != 6)
191                 && (can_int != 7) && (can_int != 9) && (can_int != 10) && 
192                 (can_int != 11) && (can_int != 12) && (can_int != 14) && 
193                 (can_int != 15)) {
194                 CANMSG("Invalid interrupt number\n");
195                 CANMSG("Valid interrupt numbers for the PIP6: 3,4,5,6,7,9,10,11,12,14 or 15\n");
196                 CANMSG("Please consult your user manual.\n");
197                 return -ENODEV;
198         }
199         switch (candevices_p[card]->io_addr) {
200                 case 0x1000: { can_addr = 0x01; break; }
201                 case 0x8000: { can_addr = 0x02; break; }
202                 case 0xe000: { can_addr = 0x03; break; }
203                 default: {
204                 CANMSG("Supplied io address is not valid, please check your manual\n");
205                 return -ENODEV;
206                 }
207         }
208         outb( (can_int<<4)|can_addr, 0x804);
209
210         return 0;
211 }
212
213 void pip_write_register(unsigned char data, unsigned long address)
214 {
215         outb(data,address);
216 }
217
218 unsigned pip_read_register(unsigned long address)
219 {
220         return inb(address);
221 }
222
223 /* !!! Don't change these functions !!! */
224 int pip5_register(struct hwspecops_t *hwspecops)
225 {
226         hwspecops->request_io = pip5_request_io;
227         hwspecops->release_io = pip5_release_io;
228         hwspecops->reset = pip_reset;
229         hwspecops->init_hw_data = pip_init_hw_data;
230         hwspecops->init_chip_data = pip_init_chip_data;
231         hwspecops->init_obj_data = pip_init_obj_data;
232         hwspecops->write_register = pip_write_register;
233         hwspecops->read_register = pip_read_register;
234         hwspecops->program_irq = pip5_program_irq;
235         return 0;
236 }
237
238 int pip6_register(struct hwspecops_t *hwspecops)
239 {
240         hwspecops->request_io = pip6_request_io;
241         hwspecops->release_io = pip6_release_io;
242         hwspecops->reset = pip_reset;
243         hwspecops->init_hw_data = pip_init_hw_data;
244         hwspecops->init_chip_data = pip_init_chip_data;
245         hwspecops->init_obj_data = pip_init_obj_data;
246         hwspecops->write_register = pip_write_register;
247         hwspecops->read_register = pip_read_register;
248         hwspecops->program_irq = pip6_program_irq;
249         return 0;
250 }