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