]> rtime.felk.cvut.cz Git - lincan.git/blob - lincan/src/pcm3680.c
The LinCAN driver license unified according to DCE FEE CTU head and superiors request.
[lincan.git] / lincan / src / pcm3680.c
1 /**************************************************************************/
2 /* File: pcm3680.c - PCM-3680 PC/104 card by Advantech                    */
3 /*                                                                        */
4 /* LinCAN - (Not only) Linux CAN bus driver                               */
5 /* Copyright (C) 2002-2009 DCE FEE CTU Prague <http://dce.felk.cvut.cz>   */
6 /* Copyright (C) 2002-2009 Pavel Pisa <pisa@cmp.felk.cvut.cz>             */
7 /* Funded by OCERA and FRESCOR IST projects                               */
8 /* Based on CAN driver code by Arnaud Westenberg <arnaud@wanadoo.nl>      */
9 /*                                                                        */
10 /* LinCAN is free software; you can redistribute it and/or modify it      */
11 /* under terms of the GNU General Public License as published by the      */
12 /* Free Software Foundation; either version 2, or (at your option) any    */
13 /* later version.  LinCAN is distributed in the hope that it will be      */
14 /* useful, but WITHOUT ANY WARRANTY; without even the implied warranty    */
15 /* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU    */
16 /* General Public License for more details. You should have received a    */
17 /* copy of the GNU General Public License along with LinCAN; see file     */
18 /* COPYING. If not, write to the Free Software Foundation, 675 Mass Ave,  */
19 /* Cambridge, MA 02139, USA.                                              */
20 /*                                                                        */
21 /* To allow use of LinCAN in the compact embedded systems firmware        */
22 /* and RT-executives (RTEMS for example), main authors agree with next    */
23 /* special exception:                                                     */
24 /*                                                                        */
25 /* Including LinCAN header files in a file, instantiating LinCAN generics */
26 /* or templates, or linking other files with LinCAN objects to produce    */
27 /* an application image/executable, does not by itself cause the          */
28 /* resulting application image/executable to be covered by                */
29 /* the GNU General Public License.                                        */
30 /* This exception does not however invalidate any other reasons           */
31 /* why the executable file might be covered by the GNU Public License.    */
32 /* Publication of enhanced or derived LinCAN files is required although.  */
33 /**************************************************************************/
34
35 #include "../include/can.h"
36 #include "../include/can_sysdep.h"
37 #include "../include/main.h"
38 #include "../include/pcm3680.h"
39 #include "../include/sja1000p.h"
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 or memory range for can board
50  * @candev: pointer to candevice/board which asks for io. Field @io_addr
51  *      of @candev is used in most cases to define start of the range
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(struct candevice_t *candev)
63 {
64         can_ioptr_t remap_addr;
65         if (!can_request_mem_region(candev->io_addr,IO_RANGE,DEVICE_NAME " - pcm3680")) {
66                 CANMSG("Unable to request IO-memory: 0x%lx\n",candev->io_addr);
67                 return -ENODEV;
68         }
69         if ( !( remap_addr = ioremap( candev->io_addr, IO_RANGE ) ) ) {
70                 CANMSG("Unable to access I/O memory at: 0x%lx\n", candev->io_addr);
71                 can_release_mem_region(candev->io_addr,IO_RANGE);
72                 return -ENODEV;
73         
74         }
75         can_base_addr_fixup(candev, remap_addr);
76         DEBUGMSG("Registered IO-memory: 0x%lx - 0x%lx\n", candev->io_addr, candev->io_addr + IO_RANGE - 1);
77         return 0;
78 }
79
80 /**
81  * template_release_io - free reserved io memory range
82  * @candev: pointer to candevice/board which releases io
83  *
84  * The function template_release_io() is used to free reserved io-memory.
85  * In case you have reserved more io memory, don't forget to free it here.
86  * IO_RANGE is the io-memory range that gets released, please adjust according
87  * your hardware. Example: #define IO_RANGE 0x100 for i82527 chips or
88  * #define IO_RANGE 0x20 for sja1000 chips in basic CAN mode.
89  * Return Value: The function always returns zero
90  * File: src/template.c
91  */
92 int pcm3680_release_io(struct candevice_t *candev)
93 {
94         iounmap((void*)candev->dev_base_addr);
95         can_release_mem_region(candev->io_addr,IO_RANGE);
96         return 0;
97 }
98
99 /**
100  * template_reset - hardware reset routine
101  * @candev: Pointer to candevice/board structure
102  *
103  * The function template_reset() is used to give a hardware reset. This is 
104  * rather hardware specific so I haven't included example code. Don't forget to 
105  * check the reset status of the chip before returning.
106  * Return Value: The function returns zero on success or %-ENODEV on failure
107  * File: src/template.c
108  */
109 int pcm3680_reset(struct candevice_t *candev)
110 {
111         int i=0;
112         struct canchip_t *chip;
113         int chipnr;
114         
115         DEBUGMSG("Resetting pcm3680 hardware ...\n");
116         for(chipnr=0;chipnr<candev->nr_sja1000_chips;chipnr++) {
117                 chip=candev->chip[chipnr];
118                 pcm3680_write_register(sjaMOD_RM, chip->chip_base_addr+SJAMOD);
119                 udelay(1000);
120                 pcm3680_write_register(0x00, chip->chip_base_addr + SJAIER);
121                 /* Write arbitrary data to reset chip */
122                 pcm3680_write_register(0x01, chip->chip_base_addr + 0x100);
123                 udelay(1000);
124                 i=20;
125                 while (pcm3680_read_register(chip->chip_base_addr+SJAMOD)&sjaMOD_RM){
126                         if(!i--) return -ENODEV;
127                         udelay(1000);
128                         pcm3680_write_register(0, chip->chip_base_addr+SJAMOD);
129                 }
130                 udelay(1000);
131                 pcm3680_write_register(sjaCDR_PELICAN, chip->chip_base_addr+SJACDR);
132                 pcm3680_write_register(0x00, chip->chip_base_addr + SJAIER);
133         }
134
135         return 0;
136 }
137
138 #define RESET_ADDR 0x100
139 #define NR_82527 0
140 #define NR_SJA1000 2
141
142 /**
143  * template_init_hw_data - Initialize hardware cards
144  * @candev: Pointer to candevice/board structure
145  *
146  * The function template_init_hw_data() is used to initialize the hardware
147  * structure containing information about the installed CAN-board.
148  * %RESET_ADDR represents the io-address of the hardware reset register.
149  * %NR_82527 represents the number of intel 82527 chips on the board.
150  * %NR_SJA1000 represents the number of philips sja1000 chips on the board.
151  * The flags entry can currently only be %CANDEV_PROGRAMMABLE_IRQ to indicate that
152  * the hardware uses programmable interrupts.
153  * Return Value: The function always returns zero
154  * File: src/template.c
155  */
156 int pcm3680_init_hw_data(struct candevice_t *candev) 
157 {
158         candev->res_addr=RESET_ADDR;
159         candev->nr_82527_chips=NR_82527;
160         candev->nr_sja1000_chips=NR_SJA1000;
161         candev->nr_all_chips=NR_82527+NR_SJA1000;
162         candev->flags &= ~CANDEV_PROGRAMMABLE_IRQ;
163
164         return 0;
165 }
166
167 /**
168  * template_init_chip_data - Initialize chips
169  * @candev: Pointer to candevice/board structure
170  * @chipnr: Number of the CAN chip on the hardware card
171  *
172  * The function template_init_chip_data() is used to initialize the hardware
173  * structure containing information about the CAN chips.
174  * %CHIP_TYPE represents the type of CAN chip. %CHIP_TYPE can be "i82527" or
175  * "sja1000".
176  * The @chip_base_addr entry represents the start of the 'official' memory map
177  * of the installed chip. It's likely that this is the same as the @io_addr
178  * argument supplied at module loading time.
179  * The @clock entry holds the chip clock value in Hz.
180  * The entry @sja_cdr_reg holds hardware specific options for the Clock Divider
181  * register. Options defined in the %sja1000.h file:
182  * %sjaCDR_CLKOUT_MASK, %sjaCDR_CLK_OFF, %sjaCDR_RXINPEN, %sjaCDR_CBP, %sjaCDR_PELICAN
183  * The entry @sja_ocr_reg holds hardware specific options for the Output Control
184  * register. Options defined in the %sja1000.h file:
185  * %sjaOCR_MODE_BIPHASE, %sjaOCR_MODE_TEST, %sjaOCR_MODE_NORMAL, %sjaOCR_MODE_CLOCK,
186  * %sjaOCR_TX0_LH, %sjaOCR_TX1_ZZ.
187  * The entry @int_clk_reg holds hardware specific options for the Clock Out
188  * register. Options defined in the %i82527.h file:
189  * %iCLK_CD0, %iCLK_CD1, %iCLK_CD2, %iCLK_CD3, %iCLK_SL0, %iCLK_SL1.
190  * The entry @int_bus_reg holds hardware specific options for the Bus 
191  * Configuration register. Options defined in the %i82527.h file:
192  * %iBUS_DR0, %iBUS_DR1, %iBUS_DT1, %iBUS_POL, %iBUS_CBY.
193  * Return Value: The function always returns zero
194  * File: src/template.c
195  */
196 int pcm3680_init_chip_data(struct candevice_t *candev, int chipnr)
197 {
198         sja1000p_fill_chipspecops(candev->chip[chipnr]);
199         candev->chip[chipnr]->chip_base_addr=
200                         candev->dev_base_addr + 0x200*chipnr;
201         candev->chip[chipnr]->clock = 16000000;
202         candev->chip[chipnr]->int_clk_reg = 0x0;
203         candev->chip[chipnr]->int_bus_reg = 0x0;
204         candev->chip[chipnr]->sja_cdr_reg = sjaCDR_CBP | sjaCDR_CLK_OFF;
205         candev->chip[chipnr]->sja_ocr_reg = sjaOCR_MODE_NORMAL |
206                                                                 sjaOCR_TX0_LH;
207
208         return 0;
209 }
210
211 /**
212  * template_init_obj_data - Initialize message buffers
213  * @chip: Pointer to chip specific structure
214  * @objnr: Number of the message buffer
215  *
216  * The function template_init_obj_data() is used to initialize the hardware
217  * structure containing information about the different message objects on the
218  * CAN chip. In case of the sja1000 there's only one message object but on the
219  * i82527 chip there are 15.
220  * The code below is for a i82527 chip and initializes the object base addresses
221  * The entry @obj_base_addr represents the first memory address of the message 
222  * object. In case of the sja1000 @obj_base_addr is taken the same as the chips
223  * base address.
224  * Unless the hardware uses a segmented memory map, flags can be set zero.
225  * Return Value: The function always returns zero
226  * File: src/template.c
227  */
228 int pcm3680_init_obj_data(struct canchip_t *chip, int objnr)
229 {
230         chip->msgobj[objnr]->obj_base_addr=chip->chip_base_addr;
231         
232         return 0;
233 }
234
235 /**
236  * template_program_irq - program interrupts
237  * @candev: Pointer to candevice/board structure
238  *
239  * The function template_program_irq() is used for hardware that uses 
240  * programmable interrupts. If your hardware doesn't use programmable interrupts
241  * you should not set the @candevices_t->flags entry to %CANDEV_PROGRAMMABLE_IRQ and 
242  * leave this function unedited. Again this function is hardware specific so 
243  * there's no example code.
244  * Return value: The function returns zero on success or %-ENODEV on failure
245  * File: src/template.c
246  */
247 int pcm3680_program_irq(struct candevice_t *candev)
248 {
249         return 0;
250 }
251
252 /**
253  * template_write_register - Low level write register routine
254  * @data: data to be written
255  * @address: memory address to write to
256  *
257  * The function template_write_register() is used to write to hardware registers
258  * on the CAN chip. You should only have to edit this function if your hardware
259  * uses some specific write process.
260  * Return Value: The function does not return a value
261  * File: src/template.c
262  */
263 void pcm3680_write_register(unsigned data, can_ioptr_t address)
264 {
265         can_writeb(data,address);
266 }
267
268 /**
269  * template_read_register - Low level read register routine
270  * @address: memory address to read from
271  *
272  * The function template_read_register() is used to read from hardware registers
273  * on the CAN chip. You should only have to edit this function if your hardware
274  * uses some specific read process.
275  * Return Value: The function returns the value stored in @address
276  * File: src/template.c
277  */
278 unsigned pcm3680_read_register(can_ioptr_t address)
279 {
280         return can_readb(address);
281 }
282
283 /* !!! Don't change this function !!! */
284 int pcm3680_register(struct hwspecops_t *hwspecops)
285 {
286         hwspecops->request_io = pcm3680_request_io;
287         hwspecops->release_io = pcm3680_release_io;
288         hwspecops->reset = pcm3680_reset;
289         hwspecops->init_hw_data = pcm3680_init_hw_data;
290         hwspecops->init_chip_data = pcm3680_init_chip_data;
291         hwspecops->init_obj_data = pcm3680_init_obj_data;
292         hwspecops->write_register = pcm3680_write_register;
293         hwspecops->read_register = pcm3680_read_register;
294         hwspecops->program_irq = pcm3680_program_irq;
295         return 0;
296 }