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