]> rtime.felk.cvut.cz Git - lincan.git/blob - lincan/src/cc_can104.c
Actual driver code for directly mapped SJA1000 into PCI mem region 0.
[lincan.git] / lincan / src / cc_can104.c
1 /**************************************************************************/
2 /* File: cc_can104.c - CAN104 PC/104 card by Contemporary Controls        */
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/cc_can104.h"
39 #include "../include/sja1000.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 0x80
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 cc104_request_io(struct candevice_t *candev)
63 {
64         if (!can_request_io_region(candev->io_addr,IO_RANGE,DEVICE_NAME)) {
65                 CANMSG("Unable to open port: 0x%lx\n",candev->io_addr);
66                 return -ENODEV;
67         }else {
68                 DEBUGMSG("Registered IO-memory: 0x%lx - 0x%lx\n", candev->io_addr, candev->io_addr + IO_RANGE - 1);
69         }
70         return 0;
71 }
72
73 /**
74  * template_elease_io - free reserved io memory range
75  * @candev: pointer to candevice/board which releases io
76  *
77  * The function template_release_io() is used to free reserved io-memory.
78  * In case you have reserved more io memory, don't forget to free it here.
79  * IO_RANGE is the io-memory range that gets released, please adjust according
80  * your hardware. Example: #define IO_RANGE 0x100 for i82527 chips or
81  * #define IO_RANGE 0x20 for sja1000 chips in basic CAN mode.
82  * Return Value: The function always returns zero
83  * File: src/template.c
84  */
85 int cc104_release_io(struct candevice_t *candev)
86 {
87         can_release_io_region(candev->io_addr,IO_RANGE);
88
89         return 0;
90 }
91
92 /**
93  * template_reset - hardware reset routine
94  * @candev: Pointer to candevice/board structure
95  *
96  * The function template_reset() is used to give a hardware reset. This is
97  * rather hardware specific so I haven't included example code. Don't forget to
98  * check the reset status of the chip before returning.
99  * Return Value: The function returns zero on success or %-ENODEV on failure
100  * File: src/template.c
101  */
102 int cc104_reset(struct candevice_t *candev)
103 {
104         return 0;
105 }
106
107 #define RESET_ADDR 0x0
108 #define NR_82527 0
109 #define NR_SJA1000 1
110
111 /**
112  * template_init_hw_data - Initialize hardware cards
113  * @candev: Pointer to candevice/board structure
114  *
115  * The function template_init_hw_data() is used to initialize the hardware
116  * structure containing information about the installed CAN-board.
117  * %RESET_ADDR represents the io-address of the hardware reset register.
118  * %NR_82527 represents the number of intel 82527 chips on the board.
119  * %NR_SJA1000 represents the number of philips sja1000 chips on the board.
120  * The flags entry can currently only be %CANDEV_PROGRAMMABLE_IRQ to indicate that
121  * the hardware uses programmable interrupts.
122  * Return Value: The function always returns zero
123  * File: src/template.c
124  */
125 int cc104_init_hw_data(struct candevice_t *candev)
126 {
127         candev->res_addr=RESET_ADDR;
128         candev->nr_82527_chips=0;
129         candev->nr_sja1000_chips=1;
130         candev->nr_all_chips=1;
131         candev->flags &= ~CANDEV_PROGRAMMABLE_IRQ;
132
133         return 0;
134 }
135
136 /**
137  * template_init_chip_data - Initialize chips
138  * @candev: Pointer to candevice/board structure
139  * @chipnr: Number of the CAN chip on the hardware card
140  *
141  * The function template_init_chip_data() is used to initialize the hardware
142  * structure containing information about the CAN chips.
143  * %CHIP_TYPE represents the type of CAN chip. %CHIP_TYPE can be "i82527" or
144  * "sja1000".
145  * The @chip_base_addr entry represents the start of the 'official' memory map
146  * of the installed chip. It's likely that this is the same as the @io_addr
147  * argument supplied at module loading time.
148  * The @clock entry holds the chip clock value in Hz.
149  * The entry @sja_cdr_reg holds hardware specific options for the Clock Divider
150  * register. Options defined in the %sja1000.h file:
151  * %sjaCDR_CLKOUT_MASK, %sjaCDR_CLK_OFF, %sjaCDR_RXINPEN, %sjaCDR_CBP, %sjaCDR_PELICAN
152  * The entry @sja_ocr_reg holds hardware specific options for the Output Control
153  * register. Options defined in the %sja1000.h file:
154  * %sjaOCR_MODE_BIPHASE, %sjaOCR_MODE_TEST, %sjaOCR_MODE_NORMAL, %sjaOCR_MODE_CLOCK,
155  * %sjaOCR_TX0_LH, %sjaOCR_TX1_ZZ.
156  * The entry @int_clk_reg holds hardware specific options for the Clock Out
157  * register. Options defined in the %i82527.h file:
158  * %iCLK_CD0, %iCLK_CD1, %iCLK_CD2, %iCLK_CD3, %iCLK_SL0, %iCLK_SL1.
159  * The entry @int_bus_reg holds hardware specific options for the Bus
160  * Configuration register. Options defined in the %i82527.h file:
161  * %iBUS_DR0, %iBUS_DR1, %iBUS_DT1, %iBUS_POL, %iBUS_CBY.
162  * Return Value: The function always returns zero
163  * File: src/template.c
164  */
165 int cc104_init_chip_data(struct candevice_t *candev, int chipnr)
166 {
167         sja1000_fill_chipspecops(candev->chip[chipnr]);
168         candev->chip[chipnr]->chip_base_addr=can_ioport2ioptr(candev->io_addr);
169         candev->chip[chipnr]->clock = 16000000;
170         candev->chip[chipnr]->flags = 0;
171         candev->chip[chipnr]->sja_cdr_reg = sjaCDR_CBP | sjaCDR_CLK_OFF;
172         candev->chip[chipnr]->sja_ocr_reg = sjaOCR_MODE_NORMAL |
173                                                                 sjaOCR_TX0_LH;
174
175         return 0;
176 }
177
178 /**
179  * template_init_obj_data - Initialize message buffers
180  * @chip: Pointer to chip specific structure
181  * @objnr: Number of the message buffer
182  *
183  * The function template_init_obj_data() is used to initialize the hardware
184  * structure containing information about the different message objects on the
185  * CAN chip. In case of the sja1000 there's only one message object but on the
186  * i82527 chip there are 15.
187  * The code below is for a i82527 chip and initializes the object base addresses
188  * The entry @obj_base_addr represents the first memory address of the message
189  * object. In case of the sja1000 @obj_base_addr is taken the same as the chips
190  * base address.
191  * Unless the hardware uses a segmented memory map, flags can be set zero.
192  * Return Value: The function always returns zero
193  * File: src/template.c
194  */
195 int cc104_init_obj_data(struct canchip_t *chip, int objnr)
196 {
197         chip->msgobj[objnr]->obj_base_addr=chip->chip_base_addr;
198
199         return 0;
200 }
201
202 /**
203  * template_program_irq - program interrupts
204  * @candev: Pointer to candevice/board structure
205  *
206  * The function template_program_irq() is used for hardware that uses
207  * programmable interrupts. If your hardware doesn't use programmable interrupts
208  * you should not set the @candevices_t->flags entry to %CANDEV_PROGRAMMABLE_IRQ and
209  * leave this function unedited. Again this function is hardware specific so
210  * there's no example code.
211  * Return value: The function returns zero on success or %-ENODEV on failure
212  * File: src/template.c
213  */
214 int cc104_program_irq(struct candevice_t *candev)
215 {
216         return 0;
217 }
218
219 /**
220  * template_write_register - Low level write register routine
221  * @data: data to be written
222  * @address: memory address to write to
223  *
224  * The function template_write_register() is used to write to hardware registers
225  * on the CAN chip. You should only have to edit this function if your hardware
226  * uses some specific write process.
227  * Return Value: The function does not return a value
228  * File: src/template.c
229  */
230 void cc104_write_register(unsigned data, can_ioptr_t address)
231 {
232         can_outb(data,address);
233 }
234
235 /**
236  * template_read_register - Low level read register routine
237  * @address: memory address to read from
238  *
239  * The function template_read_register() is used to read from hardware registers
240  * on the CAN chip. You should only have to edit this function if your hardware
241  * uses some specific read process.
242  * Return Value: The function returns the value stored in @address
243  * File: src/template.c
244  */
245 unsigned cc104_read_register(can_ioptr_t address)
246 {
247         return can_inb(address);
248 }
249
250 /* !!! Don't change this function !!! */
251 int cc104_register(struct hwspecops_t *hwspecops)
252 {
253         hwspecops->request_io = cc104_request_io;
254         hwspecops->release_io = cc104_release_io;
255         hwspecops->reset = cc104_reset;
256         hwspecops->init_hw_data = cc104_init_hw_data;
257         hwspecops->init_chip_data = cc104_init_chip_data;
258         hwspecops->init_obj_data = cc104_init_obj_data;
259         hwspecops->write_register = cc104_write_register;
260         hwspecops->read_register = cc104_read_register;
261         hwspecops->program_irq = cc104_program_irq;
262         return 0;
263 }