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