]> rtime.felk.cvut.cz Git - lincan.git/blob - lincan/src/bfadcan.c
Separated normal read and RTR assisted read transfer.
[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/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 static 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 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 canchip_t *chip=candev->chip[0];
109         unsigned cdr;
110         
111         bfadcan_write_register(sjaMOD_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|sjaCDR_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)&sjaMOD_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|sjaCDR_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 /**
165  * bfadcan_init_chip_data - Initialize chips
166  * @candev: Pointer to candevice/board structure
167  * @chipnr: Number of the CAN chip on the hardware card
168  *
169  * The function bfadcan_init_chip_data() is used to initialize the hardware
170  * structure containing information about the CAN chips.
171  * %CHIP_TYPE represents the type of CAN chip. %CHIP_TYPE can be "i82527" or
172  * "sja1000".
173  * The @chip_base_addr entry represents the start of the 'official' memory map
174  * of the installed chip. It's likely that this is the same as the @io_addr
175  * argument supplied at module loading time.
176  * The @clock entry holds the chip clock value in Hz.
177  * The entry @sja_cdr_reg holds hardware specific options for the Clock Divider
178  * register. Options defined in the %sja1000.h file:
179  * %sjaCDR_CLKOUT_MASK, %sjaCDR_CLK_OFF, %sjaCDR_RXINPEN, %sjaCDR_CBP, %sjaCDR_PELICAN
180  * The entry @sja_ocr_reg holds hardware specific options for the Output Control
181  * register. Options defined in the %sja1000.h file:
182  * %sjaOCR_MODE_BIPHASE, %sjaOCR_MODE_TEST, %sjaOCR_MODE_NORMAL, %sjaOCR_MODE_CLOCK,
183  * %sjaOCR_TX0_LH, %sjaOCR_TX1_ZZ.
184  * The entry @int_clk_reg holds hardware specific options for the Clock Out
185  * register. Options defined in the %i82527.h file:
186  * %iCLK_CD0, %iCLK_CD1, %iCLK_CD2, %iCLK_CD3, %iCLK_SL0, %iCLK_SL1.
187  * The entry @int_bus_reg holds hardware specific options for the Bus 
188  * Configuration register. Options defined in the %i82527.h file:
189  * %iBUS_DR0, %iBUS_DR1, %iBUS_DT1, %iBUS_POL, %iBUS_CBY.
190  * The entry @int_cpu_reg holds hardware specific options for the cpu interface
191  * register. Options defined in the %i82527.h file:
192  * %iCPU_CEN, %iCPU_MUX, %iCPU_SLP, %iCPU_PWD, %iCPU_DMC, %iCPU_DSC, %iCPU_RST.
193  * Return Value: The function always returns zero
194  * File: src/bfadcan.c
195  */
196 int bfadcan_init_chip_data(struct candevice_t *candev, int chipnr)
197 {
198         unsigned int id1, id2;
199         sja1000p_fill_chipspecops(candev->chip[chipnr]);
200         candev->chip[chipnr]->chip_base_addr=candev->io_addr;
201         candev->chip[chipnr]->clock = clock_freq;
202         candev->chip[chipnr]->int_cpu_reg = iCPU_DSC;
203         candev->chip[chipnr]->int_clk_reg = iCLK_SL1;
204         candev->chip[chipnr]->int_bus_reg = iBUS_CBY;
205         candev->chip[chipnr]->sja_cdr_reg = sjaCDR_CBP | sjaCDR_CLK_OFF;
206         candev->chip[chipnr]->sja_ocr_reg = sjaOCR_MODE_NORMAL |
207                                                                 sjaOCR_TX0_LH;
208         id1 = inb(0xe284);
209         id2 = inb(0xe285);
210
211
212         CANMSG("can driver ver lincan-0.3, at %04lx, CPLD v%d.%d.%d.%d\n",
213                                         candev->chip[chipnr]->chip_base_addr,
214                                                         id1>>4, id1&0x0f, id2>>4, id2&0x0f);
215
216
217         return 0;
218 }
219
220 /**
221  * bfadcan_init_obj_data - Initialize message buffers
222  * @chip: Pointer to chip specific structure
223  * @objnr: Number of the message buffer
224  *
225  * The function bfadcan_init_obj_data() is used to initialize the hardware
226  * structure containing information about the different message objects on the
227  * CAN chip. In case of the sja1000 there's only one message object but on the
228  * i82527 chip there are 15.
229  * The code below is for a i82527 chip and initializes the object base addresses
230  * The entry @obj_base_addr represents the first memory address of the message 
231  * object. In case of the sja1000 @obj_base_addr is taken the same as the chips
232  * base address.
233  * Unless the hardware uses a segmented memory map, flags can be set zero.
234  * Return Value: The function always returns zero
235  * File: src/bfadcan.c
236  */
237 int bfadcan_init_obj_data(struct canchip_t *chip, int objnr)
238 {
239         chip->msgobj[objnr]->obj_base_addr=chip->chip_base_addr+(objnr+1)*0x10;
240         
241         return 0;
242 }
243
244 /**
245  * bfadcan_program_irq - program interrupts
246  * @candev: Pointer to candevice/board structure
247  *
248  * The function bfadcan_program_irq() is used for hardware that uses 
249  * programmable interrupts. If your hardware doesn't use programmable interrupts
250  * you should not set the @candevices_t->flags entry to %CANDEV_PROGRAMMABLE_IRQ and 
251  * leave this function unedited. Again this function is hardware specific so 
252  * there's no example code.
253  * Return value: The function returns zero on success or %-ENODEV on failure
254  * File: src/bfadcan.c
255  */
256 int bfadcan_program_irq(struct candevice_t *candev)
257 {
258         return 0;
259 }
260
261 /**
262  * bfadcan_write_register - Low level write register routine
263  * @data: data to be written
264  * @address: memory address to write to
265  *
266  * The function bfadcan_write_register() is used to write to hardware registers
267  * on the CAN chip. You should only have to edit this function if your hardware
268  * uses some specific write process.
269  * Return Value: The function does not return a value
270  * File: src/bfadcan.c
271  */
272 void bfadcan_write_register(unsigned data, unsigned long address)
273 {
274 #ifdef WINDOWED_ACCESS
275         can_spin_irqflags_t flags;
276         can_spin_lock_irqsave(&bfadcan_win_lock,flags);
277         outb(address&0x00ff,0x200);
278         outb(data, 0x201);
279         can_spin_unlock_irqrestore(&bfadcan_win_lock,flags);
280 #else
281         outb(data,address);
282 #endif
283 }
284
285 /**
286  * bfadcan_read_register - Low level read register routine
287  * @address: memory address to read from
288  *
289  * The function bfadcan_read_register() is used to read from hardware registers
290  * on the CAN chip. You should only have to edit this function if your hardware
291  * uses some specific read process.
292  * Return Value: The function returns the value stored in @address
293  * File: src/bfadcan.c
294  */
295 unsigned bfadcan_read_register(unsigned long address)
296 {
297 #ifdef WINDOWED_ACCESS
298         can_spin_irqflags_t flags;
299         int ret;
300         can_spin_lock_irqsave(&bfadcan_win_lock,flags);
301         outb(address&0x00ff,0x200);
302         ret = inb(0x201);
303         can_spin_unlock_irqrestore(&bfadcan_win_lock,flags);
304         return ret;
305 #else
306         return inb(address);
307 #endif
308 }
309
310 /* !!! Don't change this function !!! */
311 int bfadcan_register(struct hwspecops_t *hwspecops)
312 {
313         hwspecops->request_io = bfadcan_request_io;
314         hwspecops->release_io = bfadcan_release_io;
315         hwspecops->reset = bfadcan_reset;
316         hwspecops->init_hw_data = bfadcan_init_hw_data;
317         hwspecops->init_chip_data = bfadcan_init_chip_data;
318         hwspecops->init_obj_data = bfadcan_init_obj_data;
319         hwspecops->write_register = bfadcan_write_register;
320         hwspecops->read_register = bfadcan_read_register;
321         hwspecops->program_irq = bfadcan_program_irq;
322         return 0;
323 }