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