]> rtime.felk.cvut.cz Git - lincan.git/blob - lincan/src/bfadcan.c
Whitespace cleanup in some LinCAN source files to prepare for integration of Jan...
[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 #define CAN_BFAD_CLOCKFREQ 20000000
27
28 /* cli and sti are not allowed in 2.5.5x SMP kernels */
29 #ifdef WINDOWED_ACCESS
30 static CAN_DEFINE_SPINLOCK(bfadcan_win_lock);
31 #endif
32
33 /*
34  * IO_RANGE is the io-memory range that gets reserved, please adjust according
35  * your hardware. Example: #define IO_RANGE 0x100 for i82527 chips or
36  * #define IO_RANGE 0x20 for sja1000 chips in basic CAN mode.
37  */
38 #ifdef WINDOWED_ACCESS
39 #define IO_RANGE 0x4
40 #else
41 #define IO_RANGE 0x100
42 #endif
43
44 unsigned bfadcan_read_register(can_ioptr_t address);
45 void bfadcan_write_register(unsigned data, can_ioptr_t address);
46
47
48 /**
49  * bfadcan_request_io: - reserve io or memory range for can board
50  * @candev: pointer to candevice/board which asks for io. Field @io_addr
51  *      of @candev is used in most cases to define start of the range
52  *
53  * The function bfadcan_request_io() is used to reserve the io-memory. If your
54  * hardware uses a dedicated memory range as hardware control registers you
55  * will have to add the code to reserve this memory as well.
56  * %IO_RANGE is the io-memory range that gets reserved, please adjust according
57  * your hardware. Example: #define IO_RANGE 0x100 for i82527 chips or
58  * #define IO_RANGE 0x20 for sja1000 chips in basic CAN mode.
59  * Return Value: The function returns zero on success or %-ENODEV on failure
60  * File: src/bfadcan.c
61  */
62 int bfadcan_request_io(struct candevice_t *candev)
63 {
64         if (!can_request_io_region(candev->io_addr,IO_RANGE,DEVICE_NAME)) {
65                 CANMSG("Unable to open port: 0x%lx\n",candev->io_addr);
66                 return -ENODEV;
67         } else {
68                 DEBUGMSG("Registered IO-memory: 0x%lx - 0x%lx\n", candev->io_addr, candev->io_addr + IO_RANGE - 1);
69         }
70         return 0;
71 }
72
73 /**
74  * bfadcan_elease_io - free reserved io memory range
75  * @candev: pointer to candevice/board which releases io
76  *
77  * The function bfadcan_release_io() is used to free reserved io-memory.
78  * In case you have reserved more io memory, don't forget to free it here.
79  * IO_RANGE is the io-memory range that gets released, please adjust according
80  * your hardware. Example: #define IO_RANGE 0x100 for i82527 chips or
81  * #define IO_RANGE 0x20 for sja1000 chips in basic CAN mode.
82  * Return Value: The function always returns zero
83  * File: src/bfadcan.c
84  */
85 int bfadcan_release_io(struct candevice_t *candev)
86 {
87         can_release_io_region(candev->io_addr,IO_RANGE);
88
89         return 0;
90 }
91
92 /**
93  * bfadcan_reset - hardware reset routine
94  * @candev: Pointer to candevice/board structure
95  *
96  * The function bfadcan_reset() is used to give a hardware reset. This is
97  * rather hardware specific so I haven't included example code. Don't forget to
98  * check the reset status of the chip before returning.
99  * Return Value: The function returns zero on success or %-ENODEV on failure
100  * File: src/bfadcan.c
101  */
102 int bfadcan_reset(struct candevice_t *candev)
103 {
104
105         int i;
106         struct canchip_t *chip=candev->chip[0];
107         unsigned cdr;
108
109         bfadcan_write_register(sjaMOD_RM, chip->chip_base_addr+SJAMOD);
110         udelay(1000);
111
112         cdr=bfadcan_read_register(chip->chip_base_addr+SJACDR);
113         bfadcan_write_register(cdr|sjaCDR_PELICAN, chip->chip_base_addr+SJACDR);
114
115         bfadcan_write_register(0, chip->chip_base_addr+SJAIER);
116
117         i=20;
118         bfadcan_write_register(0, chip->chip_base_addr+SJAMOD);
119         while (bfadcan_read_register(chip->chip_base_addr+SJAMOD)&sjaMOD_RM){
120                 if(!i--) return -ENODEV;
121                 udelay(1000);
122                 bfadcan_write_register(0, chip->chip_base_addr+SJAMOD);
123         }
124
125         cdr=bfadcan_read_register(chip->chip_base_addr+SJACDR);
126         bfadcan_write_register(cdr|sjaCDR_PELICAN, chip->chip_base_addr+SJACDR);
127
128         bfadcan_write_register(0, chip->chip_base_addr+SJAIER);
129
130         return 0;
131 }
132
133 #define RESET_ADDR 0x202
134 #define NR_82527 0
135 #define NR_SJA1000 1
136
137 /**
138  * bfadcan_init_hw_data - Initialize hardware cards
139  * @candev: Pointer to candevice/board structure
140  *
141  * The function bfadcan_init_hw_data() is used to initialize the hardware
142  * structure containing information about the installed CAN-board.
143  * %RESET_ADDR represents the io-address of the hardware reset register.
144  * %NR_82527 represents the number of intel 82527 chips on the board.
145  * %NR_SJA1000 represents the number of philips sja1000 chips on the board.
146  * The flags entry can currently only be %CANDEV_PROGRAMMABLE_IRQ to indicate that
147  * the hardware uses programmable interrupts.
148  * Return Value: The function always returns zero
149  * File: src/bfadcan.c
150  */
151 int bfadcan_init_hw_data(struct candevice_t *candev)
152 {
153         candev->res_addr=RESET_ADDR;
154         candev->nr_82527_chips=NR_82527;
155         candev->nr_sja1000_chips=NR_SJA1000;
156         candev->nr_all_chips=NR_82527+NR_SJA1000;
157         candev->flags |= 0 /* CANDEV_PROGRAMMABLE_IRQ */ ;
158
159         return 0;
160 }
161
162 /**
163  * bfadcan_init_chip_data - Initialize chips
164  * @candev: Pointer to candevice/board structure
165  * @chipnr: Number of the CAN chip on the hardware card
166  *
167  * The function bfadcan_init_chip_data() is used to initialize the hardware
168  * structure containing information about the CAN chips.
169  * %CHIP_TYPE represents the type of CAN chip. %CHIP_TYPE can be "i82527" or
170  * "sja1000".
171  * The @chip_base_addr entry represents the start of the 'official' memory map
172  * of the installed chip. It's likely that this is the same as the @io_addr
173  * argument supplied at module loading time.
174  * The @clock entry holds the chip clock value in Hz.
175  * The entry @sja_cdr_reg holds hardware specific options for the Clock Divider
176  * register. Options defined in the %sja1000.h file:
177  * %sjaCDR_CLKOUT_MASK, %sjaCDR_CLK_OFF, %sjaCDR_RXINPEN, %sjaCDR_CBP, %sjaCDR_PELICAN
178  * The entry @sja_ocr_reg holds hardware specific options for the Output Control
179  * register. Options defined in the %sja1000.h file:
180  * %sjaOCR_MODE_BIPHASE, %sjaOCR_MODE_TEST, %sjaOCR_MODE_NORMAL, %sjaOCR_MODE_CLOCK,
181  * %sjaOCR_TX0_LH, %sjaOCR_TX1_ZZ.
182  * The entry @int_clk_reg holds hardware specific options for the Clock Out
183  * register. Options defined in the %i82527.h file:
184  * %iCLK_CD0, %iCLK_CD1, %iCLK_CD2, %iCLK_CD3, %iCLK_SL0, %iCLK_SL1.
185  * The entry @int_bus_reg holds hardware specific options for the Bus
186  * Configuration register. Options defined in the %i82527.h file:
187  * %iBUS_DR0, %iBUS_DR1, %iBUS_DT1, %iBUS_POL, %iBUS_CBY.
188  * The entry @int_cpu_reg holds hardware specific options for the cpu interface
189  * register. Options defined in the %i82527.h file:
190  * %iCPU_CEN, %iCPU_MUX, %iCPU_SLP, %iCPU_PWD, %iCPU_DMC, %iCPU_DSC, %iCPU_RST.
191  * Return Value: The function always returns zero
192  * File: src/bfadcan.c
193  */
194 int bfadcan_init_chip_data(struct candevice_t *candev, int chipnr)
195 {
196         unsigned int id1, id2;
197         sja1000p_fill_chipspecops(candev->chip[chipnr]);
198         candev->chip[chipnr]->chip_base_addr=can_ioport2ioptr(candev->io_addr);
199         if(candev->chip[chipnr]->clock<=0)
200                 candev->chip[chipnr]->clock = CAN_BFAD_CLOCKFREQ;
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 = can_inb(0xe284);
204         id2 = can_inb(0xe285);
205
206
207         CANMSG("can driver ver lincan-0.3, at %04lx, CPLD v%d.%d.%d.%d\n",
208                         can_ioptr2ulong(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, can_ioptr_t address)
268 {
269 #ifdef WINDOWED_ACCESS
270         can_spin_irqflags_t flags;
271         can_spin_lock_irqsave(&bfadcan_win_lock,flags);
272         can_outb(can_ioptr2ulong(address)&0x00ff,0x200);
273         can_outb(data, 0x201);
274         can_spin_unlock_irqrestore(&bfadcan_win_lock,flags);
275 #else
276         can_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(can_ioptr_t 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         can_outb(can_ioptr2ulong(address)&0x00ff,0x200);
297         ret = can_inb(0x201);
298         can_spin_unlock_irqrestore(&bfadcan_win_lock,flags);
299         return ret;
300 #else
301         return can_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 }