]> rtime.felk.cvut.cz Git - lincan.git/blob - lincan/src/gensja1000io.c
Merge branch 'master' into can-usb1
[lincan.git] / lincan / src / gensja1000io.c
1 /* gensja1000io.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.3  17 Jun 2004
8  */ 
9
10 #include "../include/can.h"
11 #include "../include/can_sysdep.h"
12 #include "../include/main.h"
13 #include "../include/gensja1000io.h"
14 #include "../include/sja1000p.h"
15
16 /*
17  * IO_RANGE is the io-memory range that gets reserved, please adjust according
18  * your hardware. Example: #define IO_RANGE 0x100 for i82527 chips or
19  * #define IO_RANGE 0x20 for sja1000 chips in basic CAN mode.
20  */
21 #define IO_RANGE 0x20
22
23 /**
24  * gensja1000io_request_io: - reserve io or memory range for can board
25  * @candev: pointer to candevice/board which asks for io. Field @io_addr
26  *      of @candev is used in most cases to define start of the range
27  *
28  * The function gensja1000io_request_io() is used to reserve the io-memory. If your
29  * hardware uses a dedicated memory range as hardware control registers you
30  * will have to add the code to reserve this memory as well. 
31  * %IO_RANGE is the io-memory range that gets reserved, please adjust according
32  * your hardware. Example: #define IO_RANGE 0x100 for i82527 chips or
33  * #define IO_RANGE 0x20 for sja1000 chips in basic CAN mode.
34  * Return Value: The function returns zero on success or %-ENODEV on failure
35  * File: src/gensja1000io.c
36  */
37 int gensja1000io_request_io(struct candevice_t *candev)
38 {
39         if (!can_request_io_region(candev->io_addr,IO_RANGE,DEVICE_NAME)) {
40                 CANMSG("Unable to open port: 0x%lx\n",candev->io_addr);
41                 return -ENODEV;
42         }else {
43                 DEBUGMSG("Registered IO-memory: 0x%lx - 0x%lx\n", candev->io_addr, candev->io_addr + IO_RANGE - 1);
44         }
45         return 0;
46 }
47
48 /**
49  * gensja1000io_elease_io - free reserved io memory range
50  * @candev: pointer to candevice/board which releases io
51  *
52  * The function gensja1000io_release_io() is used to free reserved io-memory.
53  * In case you have reserved more io memory, don't forget to free it here.
54  * IO_RANGE is the io-memory range that gets released, please adjust according
55  * your hardware. Example: #define IO_RANGE 0x100 for i82527 chips or
56  * #define IO_RANGE 0x20 for sja1000 chips in basic CAN mode.
57  * Return Value: The function always returns zero
58  * File: src/gensja1000io.c
59  */
60 int gensja1000io_release_io(struct candevice_t *candev)
61 {
62         can_release_io_region(candev->io_addr,IO_RANGE);
63
64         return 0;
65 }
66
67 /**
68  * gensja1000io_reset - hardware reset routine
69  * @candev: Pointer to candevice/board structure
70  *
71  * The function gensja1000io_reset() is used to give a hardware reset. This is 
72  * rather hardware specific so I haven't included example code. Don't forget to 
73  * check the reset status of the chip before returning.
74  * Return Value: The function returns zero on success or %-ENODEV on failure
75  * File: src/gensja1000io.c
76  */
77 int gensja1000io_reset(struct candevice_t *candev)
78 {
79         int i;
80         struct canchip_t *chip=candev->chip[0];
81         unsigned cdr;
82         
83         gensja1000io_write_register(sjaMOD_RM, chip->chip_base_addr+SJAMOD);
84         udelay(1000);
85         
86         cdr=gensja1000io_read_register(chip->chip_base_addr+SJACDR);
87         gensja1000io_write_register(cdr|sjaCDR_PELICAN, chip->chip_base_addr+SJACDR);
88
89         gensja1000io_write_register(0, chip->chip_base_addr+SJAIER);
90
91         i=20;
92         gensja1000io_write_register(0, chip->chip_base_addr+SJAMOD);
93         while (gensja1000io_read_register(chip->chip_base_addr+SJAMOD)&sjaMOD_RM){
94                 if(!i--) return -ENODEV;
95                 udelay(1000);
96                 gensja1000io_write_register(0, chip->chip_base_addr+SJAMOD);
97         }
98
99         cdr=gensja1000io_read_register(chip->chip_base_addr+SJACDR);
100         gensja1000io_write_register(cdr|sjaCDR_PELICAN, chip->chip_base_addr+SJACDR);
101
102         gensja1000io_write_register(0, chip->chip_base_addr+SJAIER);
103         
104         return 0;
105 }
106
107 #define RESET_ADDR 0x0
108 #define NR_82527 0
109 #define NR_SJA1000 1
110
111 /**
112  * gensja1000io_init_hw_data - Initialize hardware cards
113  * @candev: Pointer to candevice/board structure
114  *
115  * The function gensja1000io_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/gensja1000io.c
124  */
125 int gensja1000io_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*0;
132
133         return 0;
134 }
135
136 /**
137  * gensja1000io_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 gensja1000io_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  * The entry @int_cpu_reg holds hardware specific options for the cpu interface
163  * register. Options defined in the %i82527.h file:
164  * %iCPU_CEN, %iCPU_MUX, %iCPU_SLP, %iCPU_PWD, %iCPU_DMC, %iCPU_DSC, %iCPU_RST.
165  * Return Value: The function always returns zero
166  * File: src/gensja1000io.c
167  */
168 int gensja1000io_init_chip_data(struct candevice_t *candev, int chipnr)
169 {
170         /*sja1000_fill_chipspecops(candev->chip[chipnr]);*/
171         sja1000p_fill_chipspecops(candev->chip[chipnr]);
172
173         candev->chip[chipnr]->chip_base_addr=can_ioport2ioptr(candev->io_addr);
174         if(candev->chip[chipnr]->clock<=0)
175         candev->chip[chipnr]->clock = 16000000;
176         candev->chip[chipnr]->int_clk_reg = 0x0;
177         candev->chip[chipnr]->int_bus_reg = 0x0;
178         candev->chip[chipnr]->sja_cdr_reg = sjaCDR_CBP | sjaCDR_CLK_OFF;
179         candev->chip[chipnr]->sja_ocr_reg = sjaOCR_MODE_NORMAL | sjaOCR_TX0_LH;
180
181         return 0;
182 }
183
184 /**
185  * gensja1000io_init_obj_data - Initialize message buffers
186  * @chip: Pointer to chip specific structure
187  * @objnr: Number of the message buffer
188  *
189  * The function gensja1000io_init_obj_data() is used to initialize the hardware
190  * structure containing information about the different message objects on the
191  * CAN chip. In case of the sja1000 there's only one message object but on the
192  * i82527 chip there are 15.
193  * The code below is for a i82527 chip and initializes the object base addresses
194  * The entry @obj_base_addr represents the first memory address of the message 
195  * object. In case of the sja1000 @obj_base_addr is taken the same as the chips
196  * base address.
197  * Unless the hardware uses a segmented memory map, flags can be set zero.
198  * Return Value: The function always returns zero
199  * File: src/gensja1000io.c
200  */
201 int gensja1000io_init_obj_data(struct canchip_t *chip, int objnr)
202 {
203         chip->msgobj[objnr]->obj_base_addr=chip->chip_base_addr;
204         return 0;
205 }
206
207 /**
208  * gensja1000io_program_irq - program interrupts
209  * @candev: Pointer to candevice/board structure
210  *
211  * The function gensja1000io_program_irq() is used for hardware that uses 
212  * programmable interrupts. If your hardware doesn't use programmable interrupts
213  * you should not set the @candevices_t->flags entry to %CANDEV_PROGRAMMABLE_IRQ and 
214  * leave this function unedited. Again this function is hardware specific so 
215  * there's no example code.
216  * Return value: The function returns zero on success or %-ENODEV on failure
217  * File: src/gensja1000io.c
218  */
219 int gensja1000io_program_irq(struct candevice_t *candev)
220 {
221         return 0;
222 }
223
224 /**
225  * gensja1000io_write_register - Low level write register routine
226  * @data: data to be written
227  * @address: memory address to write to
228  *
229  * The function gensja1000io_write_register() is used to write to hardware registers
230  * on the CAN chip. You should only have to edit this function if your hardware
231  * uses some specific write process.
232  * Return Value: The function does not return a value
233  * File: src/gensja1000io.c
234  */
235 void gensja1000io_write_register(unsigned data, can_ioptr_t address)
236 {
237         /*DEBUGMSG("gensja1000io_write_register: addr=0x%lx data=0x%x",
238                 address,data);*/
239         can_outb(data,address);
240 }
241
242 /**
243  * gensja1000io_read_register - Low level read register routine
244  * @address: memory address to read from
245  *
246  * The function gensja1000io_read_register() is used to read from hardware registers
247  * on the CAN chip. You should only have to edit this function if your hardware
248  * uses some specific read process.
249  * Return Value: The function returns the value stored in @address
250  * File: src/gensja1000io.c
251  */
252 unsigned gensja1000io_read_register(can_ioptr_t address)
253 {
254         return can_inb(address);
255 }
256
257 /* !!! Don't change this function !!! */
258 int gensja1000io_register(struct hwspecops_t *hwspecops)
259 {
260         hwspecops->request_io = gensja1000io_request_io;
261         hwspecops->release_io = gensja1000io_release_io;
262         hwspecops->reset = gensja1000io_reset;
263         hwspecops->init_hw_data = gensja1000io_init_hw_data;
264         hwspecops->init_chip_data = gensja1000io_init_chip_data;
265         hwspecops->init_obj_data = gensja1000io_init_obj_data;
266         hwspecops->write_register = gensja1000io_write_register;
267         hwspecops->read_register = gensja1000io_read_register;
268         hwspecops->program_irq = gensja1000io_program_irq;
269         return 0;
270 }