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