]> rtime.felk.cvut.cz Git - lincan.git/blob - lincan/src/bfadcan.c
The LinCAN driver license unified according to DCE FEE CTU head and superiors request.
[lincan.git] / lincan / src / bfadcan.c
1 /**************************************************************************/
2 /* File: bfadcan.c - support for BFAD can boards                          */
3 /*                                                                        */
4 /* LinCAN - (Not only) Linux CAN bus driver                               */
5 /* Copyright (C) 2002-2009 DCE FEE CTU Prague <http://dce.felk.cvut.cz>   */
6 /* Copyright (C) 2002-2009 Pavel Pisa <pisa@cmp.felk.cvut.cz>             */
7 /* Funded by OCERA and FRESCOR IST projects                               */
8 /* Based on CAN driver code by Arnaud Westenberg <arnaud@wanadoo.nl>      */
9 /*                                                                        */
10 /* LinCAN is free software; you can redistribute it and/or modify it      */
11 /* under terms of the GNU General Public License as published by the      */
12 /* Free Software Foundation; either version 2, or (at your option) any    */
13 /* later version.  LinCAN is distributed in the hope that it will be      */
14 /* useful, but WITHOUT ANY WARRANTY; without even the implied warranty    */
15 /* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU    */
16 /* General Public License for more details. You should have received a    */
17 /* copy of the GNU General Public License along with LinCAN; see file     */
18 /* COPYING. If not, write to the Free Software Foundation, 675 Mass Ave,  */
19 /* Cambridge, MA 02139, USA.                                              */
20 /*                                                                        */
21 /* To allow use of LinCAN in the compact embedded systems firmware        */
22 /* and RT-executives (RTEMS for example), main authors agree with next    */
23 /* special exception:                                                     */
24 /*                                                                        */
25 /* Including LinCAN header files in a file, instantiating LinCAN generics */
26 /* or templates, or linking other files with LinCAN objects to produce    */
27 /* an application image/executable, does not by itself cause the          */
28 /* resulting application image/executable to be covered by                */
29 /* the GNU General Public License.                                        */
30 /* This exception does not however invalidate any other reasons           */
31 /* why the executable file might be covered by the GNU Public License.    */
32 /* Publication of enhanced or derived LinCAN files is required although.  */
33 /**************************************************************************/
34
35 #define WINDOWED_ACCESS
36
37 #include "../include/can.h"
38 #include "../include/can_sysdep.h"
39 #include "../include/main.h"
40 #include "../include/sja1000p.h"
41
42 #define __NO_VERSION__
43 #include <linux/module.h>
44
45 #define CAN_BFAD_CLOCKFREQ 20000000
46
47 /* cli and sti are not allowed in 2.5.5x SMP kernels */
48 #ifdef WINDOWED_ACCESS
49 static CAN_DEFINE_SPINLOCK(bfadcan_win_lock);
50 #endif
51
52 /*
53  * IO_RANGE is the io-memory range that gets reserved, please adjust according
54  * your hardware. Example: #define IO_RANGE 0x100 for i82527 chips or
55  * #define IO_RANGE 0x20 for sja1000 chips in basic CAN mode.
56  */
57 #ifdef WINDOWED_ACCESS
58 #define IO_RANGE 0x4
59 #else
60 #define IO_RANGE 0x100
61 #endif
62
63 unsigned bfadcan_read_register(can_ioptr_t address);
64 void bfadcan_write_register(unsigned data, can_ioptr_t address);
65
66
67 /**
68  * bfadcan_request_io: - reserve io or memory range for can board
69  * @candev: pointer to candevice/board which asks for io. Field @io_addr
70  *      of @candev is used in most cases to define start of the range
71  *
72  * The function bfadcan_request_io() is used to reserve the io-memory. If your
73  * hardware uses a dedicated memory range as hardware control registers you
74  * will have to add the code to reserve this memory as well.
75  * %IO_RANGE is the io-memory range that gets reserved, please adjust according
76  * your hardware. Example: #define IO_RANGE 0x100 for i82527 chips or
77  * #define IO_RANGE 0x20 for sja1000 chips in basic CAN mode.
78  * Return Value: The function returns zero on success or %-ENODEV on failure
79  * File: src/bfadcan.c
80  */
81 int bfadcan_request_io(struct candevice_t *candev)
82 {
83         if (!can_request_io_region(candev->io_addr,IO_RANGE,DEVICE_NAME)) {
84                 CANMSG("Unable to open port: 0x%lx\n",candev->io_addr);
85                 return -ENODEV;
86         } else {
87                 DEBUGMSG("Registered IO-memory: 0x%lx - 0x%lx\n", candev->io_addr, candev->io_addr + IO_RANGE - 1);
88         }
89         return 0;
90 }
91
92 /**
93  * bfadcan_elease_io - free reserved io memory range
94  * @candev: pointer to candevice/board which releases io
95  *
96  * The function bfadcan_release_io() is used to free reserved io-memory.
97  * In case you have reserved more io memory, don't forget to free it here.
98  * IO_RANGE is the io-memory range that gets released, please adjust according
99  * your hardware. Example: #define IO_RANGE 0x100 for i82527 chips or
100  * #define IO_RANGE 0x20 for sja1000 chips in basic CAN mode.
101  * Return Value: The function always returns zero
102  * File: src/bfadcan.c
103  */
104 int bfadcan_release_io(struct candevice_t *candev)
105 {
106         can_release_io_region(candev->io_addr,IO_RANGE);
107
108         return 0;
109 }
110
111 /**
112  * bfadcan_reset - hardware reset routine
113  * @candev: Pointer to candevice/board structure
114  *
115  * The function bfadcan_reset() is used to give a hardware reset. This is
116  * rather hardware specific so I haven't included example code. Don't forget to
117  * check the reset status of the chip before returning.
118  * Return Value: The function returns zero on success or %-ENODEV on failure
119  * File: src/bfadcan.c
120  */
121 int bfadcan_reset(struct candevice_t *candev)
122 {
123
124         int i;
125         struct canchip_t *chip=candev->chip[0];
126         unsigned cdr;
127
128         bfadcan_write_register(sjaMOD_RM, chip->chip_base_addr+SJAMOD);
129         udelay(1000);
130
131         cdr=bfadcan_read_register(chip->chip_base_addr+SJACDR);
132         bfadcan_write_register(cdr|sjaCDR_PELICAN, chip->chip_base_addr+SJACDR);
133
134         bfadcan_write_register(0, chip->chip_base_addr+SJAIER);
135
136         i=20;
137         bfadcan_write_register(0, chip->chip_base_addr+SJAMOD);
138         while (bfadcan_read_register(chip->chip_base_addr+SJAMOD)&sjaMOD_RM){
139                 if(!i--) return -ENODEV;
140                 udelay(1000);
141                 bfadcan_write_register(0, chip->chip_base_addr+SJAMOD);
142         }
143
144         cdr=bfadcan_read_register(chip->chip_base_addr+SJACDR);
145         bfadcan_write_register(cdr|sjaCDR_PELICAN, chip->chip_base_addr+SJACDR);
146
147         bfadcan_write_register(0, chip->chip_base_addr+SJAIER);
148
149         return 0;
150 }
151
152 #define RESET_ADDR 0x202
153 #define NR_82527 0
154 #define NR_SJA1000 1
155
156 /**
157  * bfadcan_init_hw_data - Initialize hardware cards
158  * @candev: Pointer to candevice/board structure
159  *
160  * The function bfadcan_init_hw_data() is used to initialize the hardware
161  * structure containing information about the installed CAN-board.
162  * %RESET_ADDR represents the io-address of the hardware reset register.
163  * %NR_82527 represents the number of intel 82527 chips on the board.
164  * %NR_SJA1000 represents the number of philips sja1000 chips on the board.
165  * The flags entry can currently only be %CANDEV_PROGRAMMABLE_IRQ to indicate that
166  * the hardware uses programmable interrupts.
167  * Return Value: The function always returns zero
168  * File: src/bfadcan.c
169  */
170 int bfadcan_init_hw_data(struct candevice_t *candev)
171 {
172         candev->res_addr=RESET_ADDR;
173         candev->nr_82527_chips=NR_82527;
174         candev->nr_sja1000_chips=NR_SJA1000;
175         candev->nr_all_chips=NR_82527+NR_SJA1000;
176         candev->flags |= 0 /* CANDEV_PROGRAMMABLE_IRQ */ ;
177
178         return 0;
179 }
180
181 /**
182  * bfadcan_init_chip_data - Initialize chips
183  * @candev: Pointer to candevice/board structure
184  * @chipnr: Number of the CAN chip on the hardware card
185  *
186  * The function bfadcan_init_chip_data() is used to initialize the hardware
187  * structure containing information about the CAN chips.
188  * %CHIP_TYPE represents the type of CAN chip. %CHIP_TYPE can be "i82527" or
189  * "sja1000".
190  * The @chip_base_addr entry represents the start of the 'official' memory map
191  * of the installed chip. It's likely that this is the same as the @io_addr
192  * argument supplied at module loading time.
193  * The @clock entry holds the chip clock value in Hz.
194  * The entry @sja_cdr_reg holds hardware specific options for the Clock Divider
195  * register. Options defined in the %sja1000.h file:
196  * %sjaCDR_CLKOUT_MASK, %sjaCDR_CLK_OFF, %sjaCDR_RXINPEN, %sjaCDR_CBP, %sjaCDR_PELICAN
197  * The entry @sja_ocr_reg holds hardware specific options for the Output Control
198  * register. Options defined in the %sja1000.h file:
199  * %sjaOCR_MODE_BIPHASE, %sjaOCR_MODE_TEST, %sjaOCR_MODE_NORMAL, %sjaOCR_MODE_CLOCK,
200  * %sjaOCR_TX0_LH, %sjaOCR_TX1_ZZ.
201  * The entry @int_clk_reg holds hardware specific options for the Clock Out
202  * register. Options defined in the %i82527.h file:
203  * %iCLK_CD0, %iCLK_CD1, %iCLK_CD2, %iCLK_CD3, %iCLK_SL0, %iCLK_SL1.
204  * The entry @int_bus_reg holds hardware specific options for the Bus
205  * Configuration register. Options defined in the %i82527.h file:
206  * %iBUS_DR0, %iBUS_DR1, %iBUS_DT1, %iBUS_POL, %iBUS_CBY.
207  * The entry @int_cpu_reg holds hardware specific options for the cpu interface
208  * register. Options defined in the %i82527.h file:
209  * %iCPU_CEN, %iCPU_MUX, %iCPU_SLP, %iCPU_PWD, %iCPU_DMC, %iCPU_DSC, %iCPU_RST.
210  * Return Value: The function always returns zero
211  * File: src/bfadcan.c
212  */
213 int bfadcan_init_chip_data(struct candevice_t *candev, int chipnr)
214 {
215         unsigned int id1, id2;
216         sja1000p_fill_chipspecops(candev->chip[chipnr]);
217         candev->chip[chipnr]->chip_base_addr=can_ioport2ioptr(candev->io_addr);
218         if(candev->chip[chipnr]->clock<=0)
219                 candev->chip[chipnr]->clock = CAN_BFAD_CLOCKFREQ;
220         candev->chip[chipnr]->sja_cdr_reg = sjaCDR_CBP | sjaCDR_CLK_OFF;
221         candev->chip[chipnr]->sja_ocr_reg = sjaOCR_MODE_NORMAL | sjaOCR_TX0_LH;
222         id1 = can_inb(0xe284);
223         id2 = can_inb(0xe285);
224
225
226         CANMSG("can driver ver lincan-0.3, at %04lx, CPLD v%d.%d.%d.%d\n",
227                         can_ioptr2ulong(candev->chip[chipnr]->chip_base_addr),
228                         id1>>4, id1&0x0f, id2>>4, id2&0x0f);
229
230
231         return 0;
232 }
233
234 /**
235  * bfadcan_init_obj_data - Initialize message buffers
236  * @chip: Pointer to chip specific structure
237  * @objnr: Number of the message buffer
238  *
239  * The function bfadcan_init_obj_data() is used to initialize the hardware
240  * structure containing information about the different message objects on the
241  * CAN chip. In case of the sja1000 there's only one message object but on the
242  * i82527 chip there are 15.
243  * The code below is for a i82527 chip and initializes the object base addresses
244  * The entry @obj_base_addr represents the first memory address of the message
245  * object. In case of the sja1000 @obj_base_addr is taken the same as the chips
246  * base address.
247  * Unless the hardware uses a segmented memory map, flags can be set zero.
248  * Return Value: The function always returns zero
249  * File: src/bfadcan.c
250  */
251 int bfadcan_init_obj_data(struct canchip_t *chip, int objnr)
252 {
253         chip->msgobj[objnr]->obj_base_addr=chip->chip_base_addr+(objnr+1)*0x10;
254
255         return 0;
256 }
257
258 /**
259  * bfadcan_program_irq - program interrupts
260  * @candev: Pointer to candevice/board structure
261  *
262  * The function bfadcan_program_irq() is used for hardware that uses
263  * programmable interrupts. If your hardware doesn't use programmable interrupts
264  * you should not set the @candevices_t->flags entry to %CANDEV_PROGRAMMABLE_IRQ and
265  * leave this function unedited. Again this function is hardware specific so
266  * there's no example code.
267  * Return value: The function returns zero on success or %-ENODEV on failure
268  * File: src/bfadcan.c
269  */
270 int bfadcan_program_irq(struct candevice_t *candev)
271 {
272         return 0;
273 }
274
275 /**
276  * bfadcan_write_register - Low level write register routine
277  * @data: data to be written
278  * @address: memory address to write to
279  *
280  * The function bfadcan_write_register() is used to write to hardware registers
281  * on the CAN chip. You should only have to edit this function if your hardware
282  * uses some specific write process.
283  * Return Value: The function does not return a value
284  * File: src/bfadcan.c
285  */
286 void bfadcan_write_register(unsigned data, can_ioptr_t address)
287 {
288 #ifdef WINDOWED_ACCESS
289         can_spin_irqflags_t flags;
290         can_spin_lock_irqsave(&bfadcan_win_lock,flags);
291         can_outb(can_ioptr2ulong(address)&0x00ff,0x200);
292         can_outb(data, 0x201);
293         can_spin_unlock_irqrestore(&bfadcan_win_lock,flags);
294 #else
295         can_outb(data,address);
296 #endif
297 }
298
299 /**
300  * bfadcan_read_register - Low level read register routine
301  * @address: memory address to read from
302  *
303  * The function bfadcan_read_register() is used to read from hardware registers
304  * on the CAN chip. You should only have to edit this function if your hardware
305  * uses some specific read process.
306  * Return Value: The function returns the value stored in @address
307  * File: src/bfadcan.c
308  */
309 unsigned bfadcan_read_register(can_ioptr_t address)
310 {
311 #ifdef WINDOWED_ACCESS
312         can_spin_irqflags_t flags;
313         int ret;
314         can_spin_lock_irqsave(&bfadcan_win_lock,flags);
315         can_outb(can_ioptr2ulong(address)&0x00ff,0x200);
316         ret = can_inb(0x201);
317         can_spin_unlock_irqrestore(&bfadcan_win_lock,flags);
318         return ret;
319 #else
320         return can_inb(address);
321 #endif
322 }
323
324 /* !!! Don't change this function !!! */
325 int bfadcan_register(struct hwspecops_t *hwspecops)
326 {
327         hwspecops->request_io = bfadcan_request_io;
328         hwspecops->release_io = bfadcan_release_io;
329         hwspecops->reset = bfadcan_reset;
330         hwspecops->init_hw_data = bfadcan_init_hw_data;
331         hwspecops->init_chip_data = bfadcan_init_chip_data;
332         hwspecops->init_obj_data = bfadcan_init_obj_data;
333         hwspecops->write_register = bfadcan_write_register;
334         hwspecops->read_register = bfadcan_read_register;
335         hwspecops->program_irq = bfadcan_program_irq;
336         return 0;
337 }