]> rtime.felk.cvut.cz Git - lincan.git/blob - lincan/src/bfadcan.c
2853ed147e8ff53c1c2a03a4ef6ec27252fd5024
[lincan.git] / lincan / src / bfadcan.c
1 /* bfadcan.c
2  * Linux CAN-bus device driver.
3  * Written by Arnaud Westenberg email:arnaud@wanadoo.nl
4  * Rewritten for new CAN queues by Pavel Pisa - OCERA team member
5  * email:pisa@cmp.felk.cvut.cz
6  * This software is released under the GPL-License.
7  * Version lincan-0.2  9 Jul 2003
8  */ 
9
10 /* This file is intended as a bfadcan file for currently unsupported hardware.
11  * Once you've changed/added the functions specific to your hardware it is
12  * possible to load the driver with the hardware option hw=bfadcan.
13  */
14
15
16 #define WINDOWED_ACCESS
17
18 #include "../include/can.h"
19 #include "../include/can_sysdep.h"
20 #include "../include/main.h"
21 #include "../include/i82527.h"
22 #include "../include/sja1000p.h"
23
24 #define __NO_VERSION__
25 #include <linux/module.h>
26
27 long clock_freq;
28 MODULE_PARM(clock_freq,"i");
29
30 /* cli and sti are not allowed in 2.5.5x SMP kernels */
31 #ifdef WINDOWED_ACCESS
32 can_spinlock_t bfadcan_win_lock=SPIN_LOCK_UNLOCKED;
33 #endif
34
35 /*
36  * IO_RANGE is the io-memory range that gets reserved, please adjust according
37  * your hardware. Example: #define IO_RANGE 0x100 for i82527 chips or
38  * #define IO_RANGE 0x20 for sja1000 chips in basic CAN mode.
39  */
40 #ifdef WINDOWED_ACCESS
41 #define IO_RANGE 0x4
42 #else
43 #define IO_RANGE 0x100
44 #endif
45
46 unsigned bfadcan_read_register(unsigned long address);
47 void bfadcan_write_register(unsigned char data, unsigned long address);
48
49
50 /**
51  * bfadcan_request_io: - reserve io or memory range for can board
52  * @candev: pointer to candevice/board which asks for io. Field @io_addr
53  *      of @candev is used in most cases to define start of the range
54  *
55  * The function bfadcan_request_io() is used to reserve the io-memory. If your
56  * hardware uses a dedicated memory range as hardware control registers you
57  * will have to add the code to reserve this memory as well. 
58  * %IO_RANGE is the io-memory range that gets reserved, please adjust according
59  * your hardware. Example: #define IO_RANGE 0x100 for i82527 chips or
60  * #define IO_RANGE 0x20 for sja1000 chips in basic CAN mode.
61  * Return Value: The function returns zero on success or %-ENODEV on failure
62  * File: src/bfadcan.c
63  */
64 int bfadcan_request_io(struct candevice_t *candev)
65 {
66         if (!can_request_io_region(candev->io_addr,IO_RANGE,DEVICE_NAME)) {
67                 CANMSG("Unable to open port: 0x%lx\n",candev->io_addr);
68                 return -ENODEV;
69         } else {
70                 DEBUGMSG("Registered IO-memory: 0x%lx - 0x%lx\n", candev->io_addr, candev->io_addr + IO_RANGE - 1);
71         }
72         return 0;
73 }
74
75 /**
76  * bfadcan_elease_io - free reserved io memory range
77  * @candev: pointer to candevice/board which releases io
78  *
79  * The function bfadcan_release_io() is used to free reserved io-memory.
80  * In case you have reserved more io memory, don't forget to free it here.
81  * IO_RANGE is the io-memory range that gets released, please adjust according
82  * your hardware. Example: #define IO_RANGE 0x100 for i82527 chips or
83  * #define IO_RANGE 0x20 for sja1000 chips in basic CAN mode.
84  * Return Value: The function always returns zero
85  * File: src/bfadcan.c
86  */
87 int bfadcan_release_io(struct candevice_t *candev)
88 {
89         can_release_io_region(candev->io_addr,IO_RANGE);
90
91         return 0;
92 }
93
94 /**
95  * bfadcan_reset - hardware reset routine
96  * @candev: Pointer to candevice/board structure
97  *
98  * The function bfadcan_reset() is used to give a hardware reset. This is 
99  * rather hardware specific so I haven't included example code. Don't forget to 
100  * check the reset status of the chip before returning.
101  * Return Value: The function returns zero on success or %-ENODEV on failure
102  * File: src/bfadcan.c
103  */
104 int bfadcan_reset(struct candevice_t *candev)
105 {
106
107         int i;
108         struct chip_t *chip=candev->chip[0];
109         unsigned cdr;
110         
111         bfadcan_write_register(MOD_RM, chip->chip_base_addr+SJAMOD);
112         udelay(1000);
113         
114         cdr=bfadcan_read_register(chip->chip_base_addr+SJACDR);
115         bfadcan_write_register(cdr|CDR_PELICAN, chip->chip_base_addr+SJACDR);
116
117         bfadcan_write_register(0, chip->chip_base_addr+SJAIER);
118
119         i=20;
120         bfadcan_write_register(0, chip->chip_base_addr+SJAMOD);
121         while (bfadcan_read_register(chip->chip_base_addr+SJAMOD)&MOD_RM){
122                 if(!i--) return -ENODEV;
123                 udelay(1000);
124                 bfadcan_write_register(0, chip->chip_base_addr+SJAMOD);
125         }
126
127         cdr=bfadcan_read_register(chip->chip_base_addr+SJACDR);
128         bfadcan_write_register(cdr|CDR_PELICAN, chip->chip_base_addr+SJACDR);
129
130         bfadcan_write_register(0, chip->chip_base_addr+SJAIER);
131         
132         return 0;
133 }
134
135 #define RESET_ADDR 0x202
136 #define NR_82527 0
137 #define NR_SJA1000 1
138
139 /**
140  * bfadcan_init_hw_data - Initialize hardware cards
141  * @candev: Pointer to candevice/board structure
142  *
143  * The function bfadcan_init_hw_data() is used to initialize the hardware
144  * structure containing information about the installed CAN-board.
145  * %RESET_ADDR represents the io-address of the hardware reset register.
146  * %NR_82527 represents the number of intel 82527 chips on the board.
147  * %NR_SJA1000 represents the number of philips sja1000 chips on the board.
148  * The flags entry can currently only be %CANDEV_PROGRAMMABLE_IRQ to indicate that
149  * the hardware uses programmable interrupts.
150  * Return Value: The function always returns zero
151  * File: src/bfadcan.c
152  */
153 int bfadcan_init_hw_data(struct candevice_t *candev) 
154 {
155         candev->res_addr=RESET_ADDR;
156         candev->nr_82527_chips=NR_82527;
157         candev->nr_sja1000_chips=NR_SJA1000;
158         candev->nr_all_chips=NR_82527+NR_SJA1000;
159         candev->flags |= 0 /* CANDEV_PROGRAMMABLE_IRQ */ ;
160
161         return 0;
162 }
163
164 #define CHIP_TYPE "sja1000p"
165 /**
166  * bfadcan_init_chip_data - Initialize chips
167  * @candev: Pointer to candevice/board structure
168  * @chipnr: Number of the CAN chip on the hardware card
169  *
170  * The function bfadcan_init_chip_data() is used to initialize the hardware
171  * structure containing information about the CAN chips.
172  * %CHIP_TYPE represents the type of CAN chip. %CHIP_TYPE can be "i82527" or
173  * "sja1000".
174  * The @chip_base_addr entry represents the start of the 'official' memory map
175  * of the installed chip. It's likely that this is the same as the @io_addr
176  * argument supplied at module loading time.
177  * The @clock entry holds the chip clock value in Hz.
178  * The entry @sja_cdr_reg holds hardware specific options for the Clock Divider
179  * register. Options defined in the %sja1000.h file:
180  * %CDR_CLKOUT_MASK, %CDR_CLK_OFF, %CDR_RXINPEN, %CDR_CBP, %CDR_PELICAN
181  * The entry @sja_ocr_reg holds hardware specific options for the Output Control
182  * register. Options defined in the %sja1000.h file:
183  * %OCR_MODE_BIPHASE, %OCR_MODE_TEST, %OCR_MODE_NORMAL, %OCR_MODE_CLOCK,
184  * %OCR_TX0_LH, %OCR_TX1_ZZ.
185  * The entry @int_clk_reg holds hardware specific options for the Clock Out
186  * register. Options defined in the %i82527.h file:
187  * %iCLK_CD0, %iCLK_CD1, %iCLK_CD2, %iCLK_CD3, %iCLK_SL0, %iCLK_SL1.
188  * The entry @int_bus_reg holds hardware specific options for the Bus 
189  * Configuration register. Options defined in the %i82527.h file:
190  * %iBUS_DR0, %iBUS_DR1, %iBUS_DT1, %iBUS_POL, %iBUS_CBY.
191  * The entry @int_cpu_reg holds hardware specific options for the cpu interface
192  * register. Options defined in the %i82527.h file:
193  * %iCPU_CEN, %iCPU_MUX, %iCPU_SLP, %iCPU_PWD, %iCPU_DMC, %iCPU_DSC, %iCPU_RST.
194  * Return Value: The function always returns zero
195  * File: src/bfadcan.c
196  */
197 int bfadcan_init_chip_data(struct candevice_t *candev, int chipnr)
198 {
199         unsigned int id1, id2;
200         candev->chip[chipnr]->chip_type=CHIP_TYPE;
201         candev->chip[chipnr]->chip_base_addr=candev->io_addr;
202         candev->chip[chipnr]->clock = clock_freq;
203         candev->chip[chipnr]->int_cpu_reg = iCPU_DSC;
204         candev->chip[chipnr]->int_clk_reg = iCLK_SL1;
205         candev->chip[chipnr]->int_bus_reg = iBUS_CBY;
206         candev->chip[chipnr]->sja_cdr_reg = CDR_CBP | CDR_CLK_OFF;
207         candev->chip[chipnr]->sja_ocr_reg = OCR_MODE_NORMAL |
208                                                                 OCR_TX0_LH;
209         id1 = inb(0xe284);
210         id2 = inb(0xe285);
211
212
213         CANMSG("can driver ver lincan-0.2, at %04lx, CPLD v%d.%d.%d.%d\n",
214                                         candev->chip[chipnr]->chip_base_addr,
215                                                         id1>>4, id1&0x0f, id2>>4, id2&0x0f);
216
217
218         return 0;
219 }
220
221 /**
222  * bfadcan_init_obj_data - Initialize message buffers
223  * @chip: Pointer to chip specific structure
224  * @objnr: Number of the message buffer
225  *
226  * The function bfadcan_init_obj_data() is used to initialize the hardware
227  * structure containing information about the different message objects on the
228  * CAN chip. In case of the sja1000 there's only one message object but on the
229  * i82527 chip there are 15.
230  * The code below is for a i82527 chip and initializes the object base addresses
231  * The entry @obj_base_addr represents the first memory address of the message 
232  * object. In case of the sja1000 @obj_base_addr is taken the same as the chips
233  * base address.
234  * Unless the hardware uses a segmented memory map, flags can be set zero.
235  * Return Value: The function always returns zero
236  * File: src/bfadcan.c
237  */
238 int bfadcan_init_obj_data(struct chip_t *chip, int objnr)
239 {
240         chip->msgobj[objnr]->obj_base_addr=chip->chip_base_addr+(objnr+1)*0x10;
241         
242         return 0;
243 }
244
245 /**
246  * bfadcan_program_irq - program interrupts
247  * @candev: Pointer to candevice/board structure
248  *
249  * The function bfadcan_program_irq() is used for hardware that uses 
250  * programmable interrupts. If your hardware doesn't use programmable interrupts
251  * you should not set the @candevices_t->flags entry to %CANDEV_PROGRAMMABLE_IRQ and 
252  * leave this function unedited. Again this function is hardware specific so 
253  * there's no example code.
254  * Return value: The function returns zero on success or %-ENODEV on failure
255  * File: src/bfadcan.c
256  */
257 int bfadcan_program_irq(struct candevice_t *candev)
258 {
259         return 0;
260 }
261
262 /**
263  * bfadcan_write_register - Low level write register routine
264  * @data: data to be written
265  * @address: memory address to write to
266  *
267  * The function bfadcan_write_register() is used to write to hardware registers
268  * on the CAN chip. You should only have to edit this function if your hardware
269  * uses some specific write process.
270  * Return Value: The function does not return a value
271  * File: src/bfadcan.c
272  */
273 void bfadcan_write_register(unsigned char data, unsigned long address)
274 {
275 #ifdef WINDOWED_ACCESS
276         can_spin_irqflags_t flags;
277         can_spin_lock_irqsave(&bfadcan_win_lock,flags);
278         outb(address&0x00ff,0x200);
279         outb(data, 0x201);
280         can_spin_unlock_irqrestore(&bfadcan_win_lock,flags);
281 #else
282         outb(data,address);
283 #endif
284 }
285
286 /**
287  * bfadcan_read_register - Low level read register routine
288  * @address: memory address to read from
289  *
290  * The function bfadcan_read_register() is used to read from hardware registers
291  * on the CAN chip. You should only have to edit this function if your hardware
292  * uses some specific read process.
293  * Return Value: The function returns the value stored in @address
294  * File: src/bfadcan.c
295  */
296 unsigned bfadcan_read_register(unsigned long address)
297 {
298 #ifdef WINDOWED_ACCESS
299         can_spin_irqflags_t flags;
300         int ret;
301         can_spin_lock_irqsave(&bfadcan_win_lock,flags);
302         outb(address&0x00ff,0x200);
303         ret = inb(0x201);
304         can_spin_unlock_irqrestore(&bfadcan_win_lock,flags);
305         return ret;
306 #else
307         return inb(address);
308 #endif
309 }
310
311 /* !!! Don't change this function !!! */
312 int bfadcan_register(struct hwspecops_t *hwspecops)
313 {
314         hwspecops->request_io = bfadcan_request_io;
315         hwspecops->release_io = bfadcan_release_io;
316         hwspecops->reset = bfadcan_reset;
317         hwspecops->init_hw_data = bfadcan_init_hw_data;
318         hwspecops->init_chip_data = bfadcan_init_chip_data;
319         hwspecops->init_obj_data = bfadcan_init_obj_data;
320         hwspecops->write_register = bfadcan_write_register;
321         hwspecops->read_register = bfadcan_read_register;
322         hwspecops->program_irq = bfadcan_program_irq;
323         return 0;
324 }