]> rtime.felk.cvut.cz Git - lincan.git/blob - lincan/src/template.c
The LinCAN driver license unified according to DCE FEE CTU head and superiors request.
[lincan.git] / lincan / src / template.c
1 /**************************************************************************/
2 /* File: template.c - template file for writing new board support         */
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 /* This file is intended as a template file for currently unsupported hardware.
36  * Once you've changed/added the functions specific to your hardware it is
37  * possible to load the driver with the hardware option hw=template.
38  */
39
40 #include "../include/can.h"
41 #include "../include/can_sysdep.h"
42 #include "../include/main.h"
43 #include "../include/template.h"
44 #include "../include/i82527.h"
45 #include "../include/sja1000.h"
46
47 /*
48  * IO_RANGE is the io-memory range that gets reserved, please adjust according
49  * your hardware. Example: #define IO_RANGE 0x100 for i82527 chips or
50  * #define IO_RANGE 0x20 for sja1000 chips in basic CAN mode.
51  */
52 #define IO_RANGE 0x100
53
54 /**
55  * template_request_io: - reserve io or memory range for can board
56  * @candev: pointer to candevice/board which asks for io. Field @io_addr
57  *      of @candev is used in most cases to define start of the range
58  *
59  * The function template_request_io() is used to reserve the io-memory. If your
60  * hardware uses a dedicated memory range as hardware control registers you
61  * will have to add the code to reserve this memory as well. 
62  * %IO_RANGE is the io-memory range that gets reserved, please adjust according
63  * your hardware. Example: #define IO_RANGE 0x100 for i82527 chips or
64  * #define IO_RANGE 0x20 for sja1000 chips in basic CAN mode.
65  * Return Value: The function returns zero on success or %-ENODEV on failure
66  * File: src/template.c
67  */
68 int template_request_io(struct candevice_t *candev)
69 {
70         if (!can_request_io_region(candev->io_addr,IO_RANGE,DEVICE_NAME)) {
71                 CANMSG("Unable to open port: 0x%lx\n",candev->io_addr);
72                 return -ENODEV;
73         }else {
74                 DEBUGMSG("Registered IO-memory: 0x%lx - 0x%lx\n", candev->io_addr, candev->io_addr + IO_RANGE - 1);
75         }
76         return 0;
77 }
78
79 /**
80  * template_release_io - free reserved io memory range
81  * @candev: pointer to candevice/board which releases io
82  *
83  * The function template_release_io() is used to free reserved io-memory.
84  * In case you have reserved more io memory, don't forget to free it here.
85  * IO_RANGE is the io-memory range that gets released, please adjust according
86  * your hardware. Example: #define IO_RANGE 0x100 for i82527 chips or
87  * #define IO_RANGE 0x20 for sja1000 chips in basic CAN mode.
88  * Return Value: The function always returns zero
89  * File: src/template.c
90  */
91 int template_release_io(struct candevice_t *candev)
92 {
93         can_release_io_region(candev->io_addr,IO_RANGE);
94
95         return 0;
96 }
97
98 /**
99  * template_reset - hardware reset routine
100  * @candev: Pointer to candevice/board structure
101  *
102  * The function template_reset() is used to give a hardware reset. This is 
103  * rather hardware specific so I haven't included example code. Don't forget to 
104  * check the reset status of the chip before returning.
105  * Return Value: The function returns zero on success or %-ENODEV on failure
106  * File: src/template.c
107  */
108 int template_reset(struct candevice_t *candev)
109 {
110         return 0;
111 }
112
113 #define RESET_ADDR 0x0
114 #define NR_82527 1
115 #define NR_SJA1000 0
116
117 /**
118  * template_init_hw_data - Initialize hardware cards
119  * @candev: Pointer to candevice/board structure
120  *
121  * The function template_init_hw_data() is used to initialize the hardware
122  * structure containing information about the installed CAN-board.
123  * %RESET_ADDR represents the io-address of the hardware reset register.
124  * %NR_82527 represents the number of Intel 82527 chips on the board.
125  * %NR_SJA1000 represents the number of Philips sja1000 chips on the board.
126  * The flags entry can currently only be %CANDEV_PROGRAMMABLE_IRQ to indicate that
127  * the hardware uses programmable interrupts.
128  * Return Value: The function always returns zero
129  * File: src/template.c
130  */
131 int template_init_hw_data(struct candevice_t *candev) 
132 {
133         candev->res_addr=RESET_ADDR;
134         candev->nr_82527_chips=NR_82527;
135         candev->nr_sja1000_chips=NR_SJA1000;
136         candev->nr_all_chips=NR_82527+NR_SJA1000;
137         candev->flags |= CANDEV_PROGRAMMABLE_IRQ;
138
139         return 0;
140 }
141
142 /**
143  * template_init_chip_data - Initialize chips
144  * @candev: Pointer to candevice/board structure
145  * @chipnr: Number of the CAN chip on the hardware card
146  *
147  * The function template_init_chip_data() is used to initialize the hardware
148  * structure containing information about the CAN chips.
149  * %CHIP_TYPE represents the type of CAN chip. %CHIP_TYPE can be "i82527" or
150  * "sja1000".
151  * The @chip_base_addr entry represents the start of the 'official' memory map
152  * of the installed chip. It's likely that this is the same as the @io_addr
153  * argument supplied at module loading time.
154  * The @clock entry holds the chip clock value in Hz.
155  * The entry @sja_cdr_reg holds hardware specific options for the Clock Divider
156  * register. Options defined in the %sja1000.h file:
157  * %sjaCDR_CLKOUT_MASK, %sjaCDR_CLK_OFF, %sjaCDR_RXINPEN, %sjaCDR_CBP, %sjaCDR_PELICAN
158  * The entry @sja_ocr_reg holds hardware specific options for the Output Control
159  * register. Options defined in the %sja1000.h file:
160  * %sjaOCR_MODE_BIPHASE, %sjaOCR_MODE_TEST, %sjaOCR_MODE_NORMAL, %sjaOCR_MODE_CLOCK,
161  * %sjaOCR_TX0_LH, %sjaOCR_TX1_ZZ.
162  * The entry @int_clk_reg holds hardware specific options for the Clock Out
163  * register. Options defined in the %i82527.h file:
164  * %iCLK_CD0, %iCLK_CD1, %iCLK_CD2, %iCLK_CD3, %iCLK_SL0, %iCLK_SL1.
165  * The entry @int_bus_reg holds hardware specific options for the Bus 
166  * Configuration register. Options defined in the %i82527.h file:
167  * %iBUS_DR0, %iBUS_DR1, %iBUS_DT1, %iBUS_POL, %iBUS_CBY.
168  * The entry @int_cpu_reg holds hardware specific options for the cpu interface
169  * register. Options defined in the %i82527.h file:
170  * %iCPU_CEN, %iCPU_MUX, %iCPU_SLP, %iCPU_PWD, %iCPU_DMC, %iCPU_DSC, %iCPU_RST.
171  * Return Value: The function always returns zero
172  * File: src/template.c
173  */
174 int template_init_chip_data(struct candevice_t *candev, int chipnr)
175 {
176         i82527_fill_chipspecops(candev->chip[chipnr]);
177         /*sja1000_fill_chipspecops(candev->chip[chipnr]);*/
178         /*sja1000p_fill_chipspecops(candev->chip[chipnr]);*/
179         
180         candev->chip[chipnr]->chip_base_addr=can_ioport2ioptr(candev->io_addr);
181         candev->chip[chipnr]->clock = 16000000;
182         candev->chip[chipnr]->int_cpu_reg = iCPU_DSC;
183         candev->chip[chipnr]->int_clk_reg = iCLK_SL1;
184         candev->chip[chipnr]->int_bus_reg = iBUS_CBY;
185         candev->chip[chipnr]->sja_cdr_reg = sjaCDR_CBP | sjaCDR_CLK_OFF;
186         candev->chip[chipnr]->sja_ocr_reg = sjaOCR_MODE_NORMAL |
187                                                                 sjaOCR_TX0_LH;
188
189         return 0;
190 }
191
192 /**
193  * template_init_obj_data - Initialize message buffers
194  * @chip: Pointer to chip specific structure
195  * @objnr: Number of the message buffer
196  *
197  * The function template_init_obj_data() is used to initialize the hardware
198  * structure containing information about the different message objects on the
199  * CAN chip. In case of the sja1000 there's only one message object but on the
200  * i82527 chip there are 15.
201  * The code below is for a i82527 chip and initializes the object base addresses
202  * The entry @obj_base_addr represents the first memory address of the message 
203  * object. In case of the sja1000 @obj_base_addr is taken the same as the chips
204  * base address.
205  * Unless the hardware uses a segmented memory map, flags can be set zero.
206  * Return Value: The function always returns zero
207  * File: src/template.c
208  */
209 int template_init_obj_data(struct canchip_t *chip, int objnr)
210 {
211         chip->msgobj[objnr]->obj_base_addr=chip->chip_base_addr+(objnr+1)*0x10;
212         
213         return 0;
214 }
215
216 /**
217  * template_program_irq - program interrupts
218  * @candev: Pointer to candevice/board structure
219  *
220  * The function template_program_irq() is used for hardware that uses 
221  * programmable interrupts. If your hardware doesn't use programmable interrupts
222  * you should not set the @candevices_t->flags entry to %CANDEV_PROGRAMMABLE_IRQ and 
223  * leave this function unedited. Again this function is hardware specific so 
224  * there's no example code.
225  * Return value: The function returns zero on success or %-ENODEV on failure
226  * File: src/template.c
227  */
228 int template_program_irq(struct candevice_t *candev)
229 {
230         return 0;
231 }
232
233 /**
234  * template_write_register - Low level write register routine
235  * @data: data to be written
236  * @address: memory address to write to
237  *
238  * The function template_write_register() is used to write to hardware registers
239  * on the CAN chip. You should only have to edit this function if your hardware
240  * uses some specific write process.
241  * Return Value: The function does not return a value
242  * File: src/template.c
243  */
244 void template_write_register(unsigned data, can_ioptr_t address)
245 {
246         can_outb(data,address);
247 }
248
249 /**
250  * template_read_register - Low level read register routine
251  * @address: memory address to read from
252  *
253  * The function template_read_register() is used to read from hardware registers
254  * on the CAN chip. You should only have to edit this function if your hardware
255  * uses some specific read process.
256  * Return Value: The function returns the value stored in @address
257  * File: src/template.c
258  */
259 unsigned template_read_register(can_ioptr_t address)
260 {
261         return can_inb(address);
262 }
263
264 /* !!! Don't change this function !!! */
265 int template_register(struct hwspecops_t *hwspecops)
266 {
267         hwspecops->request_io = template_request_io;
268         hwspecops->release_io = template_release_io;
269         hwspecops->reset = template_reset;
270         hwspecops->init_hw_data = template_init_hw_data;
271         hwspecops->init_chip_data = template_init_chip_data;
272         hwspecops->init_obj_data = template_init_obj_data;
273         hwspecops->write_register = template_write_register;
274         hwspecops->read_register = template_read_register;
275         hwspecops->program_irq = template_program_irq;
276         return 0;
277 }