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