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