]> rtime.felk.cvut.cz Git - lincan.git/blob - lincan/src/template.c
The first enhanced version of Linux CAN-bus driver for OCERA project
[lincan.git] / lincan / src / template.c
1 /* template.c
2  * Linux CAN-bus device driver.
3  * Written by Arnaud Westenberg email:arnaud@wanadoo.nl
4  * This software is released under the GPL-License.
5  * Version 0.7  6 Aug 2001
6  */ 
7
8 /* This file is intended as a template file for currently unsupported hardware.
9  * Once you've changed/added the functions specific to your hardware it is
10  * possible to load the driver with the hardware option hw=template.
11  */
12
13 #include <linux/autoconf.h>
14 #if defined (CONFIG_MODVERSIONS) && !defined (MODVERSIONS)
15 #define MODVERSIONS
16 #endif
17
18 #if defined (MODVERSIONS)
19 #include <linux/modversions.h>
20 #endif
21
22 #include <linux/ioport.h>
23 #include <linux/delay.h>
24 #include <asm/errno.h>
25 #include <asm/io.h>
26
27 #include "../include/main.h"
28 #include "../include/template.h"
29 #include "../include/i82527.h"
30 #include "../include/sja1000.h"
31
32 /*
33  * IO_RANGE is the io-memory range that gets reserved, please adjust according
34  * your hardware. Example: #define IO_RANGE 0x100 for i82527 chips or
35  * #define IO_RANGE 0x20 for sja1000 chips in basic CAN mode.
36  */
37 #define IO_RANGE 0x100
38
39 /**
40  * template_request_io: - reserve io memory
41  * @io_addr: The reserved memory starts at @io_addr, wich is the module 
42  * parameter @io.
43  *
44  * The function template_request_io() is used to reserve the io-memory. If your
45  * hardware uses a dedicated memory range as hardware control registers you
46  * will have to add the code to reserve this memory as well. 
47  * %IO_RANGE is the io-memory range that gets reserved, please adjust according
48  * your hardware. Example: #define IO_RANGE 0x100 for i82527 chips or
49  * #define IO_RANGE 0x20 for sja1000 chips in basic CAN mode.
50  * Return Value: The function returns zero on success or %-ENODEV on failure
51  * File: src/template.c
52  */
53 int template_request_io(unsigned long io_addr)
54 {
55         if (check_region(io_addr,IO_RANGE)) {
56                 CANMSG("Unable to open port: 0x%lx\n",io_addr);
57                 return -ENODEV;
58         }
59         else {
60                 request_region(io_addr,IO_RANGE,DEVICE_NAME);
61                 DEBUGMSG("Registered IO-memory: 0x%lx - 0x%lx\n", io_addr, io_addr + IO_RANGE - 1);
62         }
63         return 0;
64 }
65
66 /**
67  * template_release_io - free reserved io-memory
68  * @io_addr: Start of the memory range to be released.
69  *
70  * The function template_release_io() is used to free reserved io-memory.
71  * In case you have reserved more io memory, don't forget to free it here.
72  * IO_RANGE is the io-memory range that gets released, please adjust according
73  * your hardware. Example: #define IO_RANGE 0x100 for i82527 chips or
74  * #define IO_RANGE 0x20 for sja1000 chips in basic CAN mode.
75  * Return Value: The function always returns zero
76  * File: src/template.c
77  */
78 int template_release_io(unsigned long io_addr)
79 {
80         release_region(io_addr,IO_RANGE);
81
82         return 0;
83 }
84
85 /**
86  * template_reset - hardware reset routine
87  * @card: Number of the hardware card.
88  *
89  * The function template_reset() is used to give a hardware reset. This is 
90  * rather hardware specific so I haven't included example code. Don't forget to 
91  * check the reset status of the chip before returning.
92  * Return Value: The function returns zero on success or %-ENODEV on failure
93  * File: src/template.c
94  */
95 int template_reset(int card)
96 {
97         return 0;
98 }
99
100 #define RESET_ADDR 0x0
101 #define NR_82527 0
102 #define NR_SJA1000 0
103
104 /**
105  * template_init_hw_data - Initialze hardware cards
106  * @card: Number of the hardware card.
107  *
108  * The function template_init_hw_data() is used to initialize the hardware
109  * structure containing information about the installed CAN-board.
110  * %RESET_ADDR represents the io-address of the hardware reset register.
111  * %NR_82527 represents the number of intel 82527 chips on the board.
112  * %NR_SJA1000 represents the number of philips sja1000 chips on the board.
113  * The flags entry can currently only be %PROGRAMMABLE_IRQ to indicate that
114  * the hardware uses programmable interrupts.
115  * Return Value: The function always returns zero
116  * File: src/template.c
117  */
118 int template_init_hw_data(int card) 
119 {
120         candevices_p[card]->res_addr=RESET_ADDR;
121         candevices_p[card]->nr_82527_chips=1;
122         candevices_p[card]->nr_sja1000_chips=0;
123         candevices_p[card]->flags |= PROGRAMMABLE_IRQ;
124
125         return 0;
126 }
127
128 #define CHIP_TYPE "i82527"
129 /**
130  * template_init_chip_data - Initialize chips
131  * @card: Number of the hardware card
132  * @chipnr: Number of the CAN chip on the hardware card
133  *
134  * The function template_init_chip_data() is used to initialize the hardware
135  * structure containing information about the CAN chips.
136  * %CHIP_TYPE represents the type of CAN chip. %CHIP_TYPE can be "i82527" or
137  * "sja1000".
138  * The @chip_base_addr entry represents the start of the 'official' memory map
139  * of the installed chip. It's likely that this is the same as the @io_addr
140  * argument supplied at module loading time.
141  * The @clock entry holds the chip clock value in Hz.
142  * The entry @sja_cdr_reg holds hardware specific options for the Clock Divider
143  * register. Options defined in the %sja1000.h file:
144  * %CDR_CLKOUT_MASK, %CDR_CLK_OFF, %CDR_RXINPEN, %CDR_CBP, %CDR_PELICAN
145  * The entry @sja_ocr_reg holds hardware specific options for the Output Control
146  * register. Options defined in the %sja1000.h file:
147  * %OCR_MODE_BIPHASE, %OCR_MODE_TEST, %OCR_MODE_NORMAL, %OCR_MODE_CLOCK,
148  * %OCR_TX0_LH, %OCR_TX1_ZZ.
149  * The entry @int_clk_reg holds hardware specific options for the Clock Out
150  * register. Options defined in the %i82527.h file:
151  * %iCLK_CD0, %iCLK_CD1, %iCLK_CD2, %iCLK_CD3, %iCLK_SL0, %iCLK_SL1.
152  * The entry @int_bus_reg holds hardware specific options for the Bus 
153  * Configuration register. Options defined in the %i82527.h file:
154  * %iBUS_DR0, %iBUS_DR1, %iBUS_DT1, %iBUS_POL, %iBUS_CBY.
155  * The entry @int_cpu_reg holds hardware specific options for the cpu interface
156  * register. Options defined in the %i82527.h file:
157  * %iCPU_CEN, %iCPU_MUX, %iCPU_SLP, %iCPU_PWD, %iCPU_DMC, %iCPU_DSC, %iCPU_RST.
158  * Return Value: The function always returns zero
159  * File: src/template.c
160  */
161 int template_init_chip_data(int card, int chipnr)
162 {
163         candevices_p[card]->chip[chipnr]->chip_type=CHIP_TYPE;
164         candevices_p[card]->chip[chipnr]->chip_base_addr=candevices_p[card]->io_addr;
165         candevices_p[card]->chip[chipnr]->clock = 16000000;
166         candevices_p[card]->chip[chipnr]->int_cpu_reg = iCPU_DSC;
167         candevices_p[card]->chip[chipnr]->int_clk_reg = iCLK_SL1;
168         candevices_p[card]->chip[chipnr]->int_bus_reg = iBUS_CBY;
169         candevices_p[card]->chip[chipnr]->sja_cdr_reg = CDR_CBP | CDR_CLK_OFF;
170         candevices_p[card]->chip[chipnr]->sja_ocr_reg = OCR_MODE_NORMAL |
171                                                                 OCR_TX0_LH;
172
173         return 0;
174 }
175
176 /**
177  * template_init_obj_data - Initialize message buffers
178  * @chipnr: Number of the CAN chip
179  * @objnr: Number of the message buffer
180  *
181  * The function template_init_obj_data() is used to initialize the hardware
182  * structure containing information about the different message objects on the
183  * CAN chip. In case of the sja1000 there's only one message object but on the
184  * i82527 chip there are 15.
185  * The code below is for a i82527 chip and initializes the object base addresses
186  * The entry @obj_base_addr represents the first memory address of the message 
187  * object. In case of the sja1000 @obj_base_addr is taken the same as the chips
188  * base address.
189  * Unless the hardware uses a segmented memory map, flags can be set zero.
190  * Return Value: The function always returns zero
191  * File: src/template.c
192  */
193 int template_init_obj_data(int chipnr, int objnr)
194 {
195         chips_p[chipnr]->msgobj[objnr]->obj_base_addr=chips_p[chipnr]->chip_base_addr+(objnr+1)*0x10;
196         chips_p[chipnr]->msgobj[objnr]->flags=0;
197         
198         return 0;
199 }
200
201 /**
202  * template_program_irq - program interrupts
203  * @card: Number of the hardware card.
204  *
205  * The function template_program_irq() is used for hardware that uses 
206  * programmable interrupts. If your hardware doesn't use programmable interrupts
207  * you should not set the @candevices_t->flags entry to %PROGRAMMABLE_IRQ and 
208  * leave this function unedited. Again this function is hardware specific so 
209  * there's no example code.
210  * Return value: The function returns zero on success or %-ENODEV on failure
211  * File: src/template.c
212  */
213 int template_program_irq(int card)
214 {
215         return 0;
216 }
217
218 /**
219  * template_write_register - Low level write register routine
220  * @data: data to be written
221  * @address: memory address to write to
222  *
223  * The function template_write_register() is used to write to hardware registers
224  * on the CAN chip. You should only have to edit this function if your hardware
225  * uses some specific write process.
226  * Return Value: The function does not return a value
227  * File: src/template.c
228  */
229 void template_write_register(unsigned char data, unsigned long address)
230 {
231         outb(data,address);
232 }
233
234 /**
235  * template_read_register - Low level read register routine
236  * @address: memory address to read from
237  *
238  * The function template_read_register() is used to read from hardware registers
239  * on the CAN chip. You should only have to edit this function if your hardware
240  * uses some specific read process.
241  * Return Value: The function returns the value stored in @address
242  * File: src/template.c
243  */
244 unsigned template_read_register(unsigned long address)
245 {
246         return inb(address);
247 }
248
249 /* !!! Don't change this function !!! */
250 int template_register(struct hwspecops_t *hwspecops)
251 {
252         hwspecops->request_io = template_request_io;
253         hwspecops->release_io = template_release_io;
254         hwspecops->reset = template_reset;
255         hwspecops->init_hw_data = template_init_hw_data;
256         hwspecops->init_chip_data = template_init_chip_data;
257         hwspecops->init_obj_data = template_init_obj_data;
258         hwspecops->write_register = template_write_register;
259         hwspecops->read_register = template_read_register;
260         hwspecops->program_irq = template_program_irq;
261         return 0;
262 }