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