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