]> rtime.felk.cvut.cz Git - lincan.git/blob - lincan/src/pikronisa.c
93c2b293d78f330e83c5189f45eb654a6a140dc9
[lincan.git] / lincan / src / pikronisa.c
1 /* pikronisa.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/pikronisa.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 0x100
23
24 /**
25  * pikronisa_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 pikronisa_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/pikronisa.c
37  */
38 int pikronisa_request_io(struct candevice_t *candev)
39 {
40         int remap_addr;
41         
42         if (!can_request_mem_region(candev->io_addr,IO_RANGE,DEVICE_NAME " - pikronisa")) {
43                 CANMSG("Unable to request IO-memory: 0x%lx\n",candev->io_addr);
44                 return -ENODEV;
45         }
46         if ( !( remap_addr = (long) ioremap( candev->io_addr, IO_RANGE ) ) ) {
47                 CANMSG("Unable to access I/O memory at: 0x%lx\n", candev->io_addr);
48                 can_release_mem_region(candev->io_addr,IO_RANGE);
49                 return -ENODEV;
50         
51         }
52         can_base_addr_fixup(candev, remap_addr);
53         DEBUGMSG("Registered IO-memory: 0x%lx - 0x%lx\n", candev->io_addr, candev->io_addr + IO_RANGE - 1);
54         return 0;
55 }
56
57 /**
58  * pikronisa_elease_io - free reserved io memory range
59  * @candev: pointer to candevice/board which releases io
60  *
61  * The function pikronisa_release_io() is used to free reserved io-memory.
62  * In case you have reserved more io memory, don't forget to free it here.
63  * IO_RANGE is the io-memory range that gets released, please adjust according
64  * your hardware. Example: #define IO_RANGE 0x100 for i82527 chips or
65  * #define IO_RANGE 0x20 for sja1000 chips in basic CAN mode.
66  * Return Value: The function always returns zero
67  * File: src/pikronisa.c
68  */
69 int pikronisa_release_io(struct candevice_t *candev)
70 {
71         /* release I/O memory mapping */
72         iounmap((void*)candev->dev_base_addr);
73         can_release_mem_region(candev->io_addr,IO_RANGE);
74
75         return 0;
76 }
77
78 /**
79  * pikronisa_reset - hardware reset routine
80  * @candev: Pointer to candevice/board structure
81  *
82  * The function pikronisa_reset() is used to give a hardware reset. This is 
83  * rather hardware specific so I haven't included example code. Don't forget to 
84  * check the reset status of the chip before returning.
85  * Return Value: The function returns zero on success or %-ENODEV on failure
86  * File: src/pikronisa.c
87  */
88 int pikronisa_reset(struct candevice_t *candev)
89 {
90         int i;
91         struct chip_t *chip=candev->chip[0];
92         unsigned cdr;
93         
94         pikronisa_write_register(sjaMOD_RM, chip->chip_base_addr+SJAMOD);
95         udelay(1000);
96         
97         cdr=pikronisa_read_register(chip->chip_base_addr+SJACDR);
98         pikronisa_write_register(cdr|sjaCDR_PELICAN, chip->chip_base_addr+SJACDR);
99
100         pikronisa_write_register(0, chip->chip_base_addr+SJAIER);
101
102         i=20;
103         pikronisa_write_register(0, chip->chip_base_addr+SJAMOD);
104         while (pikronisa_read_register(chip->chip_base_addr+SJAMOD)&sjaMOD_RM){
105                 if(!i--) return -ENODEV;
106                 udelay(1000);
107                 pikronisa_write_register(0, chip->chip_base_addr+SJAMOD);
108         }
109
110         cdr=pikronisa_read_register(chip->chip_base_addr+SJACDR);
111         pikronisa_write_register(cdr|sjaCDR_PELICAN, chip->chip_base_addr+SJACDR);
112
113         pikronisa_write_register(0, chip->chip_base_addr+SJAIER);
114         
115         return 0;
116 }
117
118 #define RESET_ADDR 0x0
119 #define NR_82527 0
120 #define NR_SJA1000 1
121
122 /**
123  * pikronisa_init_hw_data - Initialize hardware cards
124  * @candev: Pointer to candevice/board structure
125  *
126  * The function pikronisa_init_hw_data() is used to initialize the hardware
127  * structure containing information about the installed CAN-board.
128  * %RESET_ADDR represents the io-address of the hardware reset register.
129  * %NR_82527 represents the number of intel 82527 chips on the board.
130  * %NR_SJA1000 represents the number of philips sja1000 chips on the board.
131  * The flags entry can currently only be %CANDEV_PROGRAMMABLE_IRQ to indicate that
132  * the hardware uses programmable interrupts.
133  * Return Value: The function always returns zero
134  * File: src/pikronisa.c
135  */
136 int pikronisa_init_hw_data(struct candevice_t *candev) 
137 {
138         candev->res_addr=RESET_ADDR;
139         candev->nr_82527_chips=0;
140         candev->nr_sja1000_chips=1;
141         candev->nr_all_chips=1;
142         candev->flags |= CANDEV_PROGRAMMABLE_IRQ*0;
143
144         return 0;
145 }
146
147 #define CHIP_TYPE "sja1000p"
148 /* #define CHIP_TYPE "sja1000" */
149
150 /**
151  * pikronisa_init_chip_data - Initialize chips
152  * @candev: Pointer to candevice/board structure
153  * @chipnr: Number of the CAN chip on the hardware card
154  *
155  * The function pikronisa_init_chip_data() is used to initialize the hardware
156  * structure containing information about the CAN chips.
157  * %CHIP_TYPE represents the type of CAN chip. %CHIP_TYPE can be "i82527" or
158  * "sja1000".
159  * The @chip_base_addr entry represents the start of the 'official' memory map
160  * of the installed chip. It's likely that this is the same as the @io_addr
161  * argument supplied at module loading time.
162  * The @clock entry holds the chip clock value in Hz.
163  * The entry @sja_cdr_reg holds hardware specific options for the Clock Divider
164  * register. Options defined in the %sja1000.h file:
165  * %sjaCDR_CLKOUT_MASK, %sjaCDR_CLK_OFF, %sjaCDR_RXINPEN, %sjaCDR_CBP, %sjaCDR_PELICAN
166  * The entry @sja_ocr_reg holds hardware specific options for the Output Control
167  * register. Options defined in the %sja1000.h file:
168  * %sjaOCR_MODE_BIPHASE, %sjaOCR_MODE_TEST, %sjaOCR_MODE_NORMAL, %sjaOCR_MODE_CLOCK,
169  * %sjaOCR_TX0_LH, %sjaOCR_TX1_ZZ.
170  * The entry @int_clk_reg holds hardware specific options for the Clock Out
171  * register. Options defined in the %i82527.h file:
172  * %iCLK_CD0, %iCLK_CD1, %iCLK_CD2, %iCLK_CD3, %iCLK_SL0, %iCLK_SL1.
173  * The entry @int_bus_reg holds hardware specific options for the Bus 
174  * Configuration register. Options defined in the %i82527.h file:
175  * %iBUS_DR0, %iBUS_DR1, %iBUS_DT1, %iBUS_POL, %iBUS_CBY.
176  * The entry @int_cpu_reg holds hardware specific options for the cpu interface
177  * register. Options defined in the %i82527.h file:
178  * %iCPU_CEN, %iCPU_MUX, %iCPU_SLP, %iCPU_PWD, %iCPU_DMC, %iCPU_DSC, %iCPU_RST.
179  * Return Value: The function always returns zero
180  * File: src/pikronisa.c
181  */
182 int pikronisa_init_chip_data(struct candevice_t *candev, int chipnr)
183 {
184         candev->chip[chipnr]->chip_type=CHIP_TYPE;
185         candev->chip[chipnr]->chip_base_addr=candev->io_addr;
186         candev->chip[chipnr]->clock = 24000000;
187         candev->chip[chipnr]->int_clk_reg = 0x0;
188         candev->chip[chipnr]->int_bus_reg = 0x0;
189         candev->chip[chipnr]->sja_cdr_reg = sjaCDR_CBP | sjaCDR_CLK_OFF;
190         candev->chip[chipnr]->sja_ocr_reg = sjaOCR_MODE_NORMAL | sjaOCR_TX0_LH;
191
192         return 0;
193 }
194
195 /**
196  * pikronisa_init_obj_data - Initialize message buffers
197  * @chip: Pointer to chip specific structure
198  * @objnr: Number of the message buffer
199  *
200  * The function pikronisa_init_obj_data() is used to initialize the hardware
201  * structure containing information about the different message objects on the
202  * CAN chip. In case of the sja1000 there's only one message object but on the
203  * i82527 chip there are 15.
204  * The code below is for a i82527 chip and initializes the object base addresses
205  * The entry @obj_base_addr represents the first memory address of the message 
206  * object. In case of the sja1000 @obj_base_addr is taken the same as the chips
207  * base address.
208  * Unless the hardware uses a segmented memory map, flags can be set zero.
209  * Return Value: The function always returns zero
210  * File: src/pikronisa.c
211  */
212 int pikronisa_init_obj_data(struct chip_t *chip, int objnr)
213 {
214         chip->msgobj[objnr]->obj_base_addr=chip->chip_base_addr;
215         return 0;
216 }
217
218 /**
219  * pikronisa_program_irq - program interrupts
220  * @candev: Pointer to candevice/board structure
221  *
222  * The function pikronisa_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 %CANDEV_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/pikronisa.c
229  */
230 int pikronisa_program_irq(struct candevice_t *candev)
231 {
232         return 0;
233 }
234
235 /**
236  * pikronisa_write_register - Low level write register routine
237  * @data: data to be written
238  * @address: memory address to write to
239  *
240  * The function pikronisa_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/pikronisa.c
245  */
246 void pikronisa_write_register(unsigned data, unsigned long address)
247 {
248         /*DEBUGMSG("pikronisa_write_register: addr=0x%lx data=0x%x",
249                 address,data);*/
250         writeb(data,address);
251 }
252
253 /**
254  * pikronisa_read_register - Low level read register routine
255  * @address: memory address to read from
256  *
257  * The function pikronisa_read_register() is used to read from hardware registers
258  * on the CAN chip. You should only have to edit this function if your hardware
259  * uses some specific read process.
260  * Return Value: The function returns the value stored in @address
261  * File: src/pikronisa.c
262  */
263 unsigned pikronisa_read_register(unsigned long address)
264 {
265         return readb(address);
266 }
267
268 /* !!! Don't change this function !!! */
269 int pikronisa_register(struct hwspecops_t *hwspecops)
270 {
271         hwspecops->request_io = pikronisa_request_io;
272         hwspecops->release_io = pikronisa_release_io;
273         hwspecops->reset = pikronisa_reset;
274         hwspecops->init_hw_data = pikronisa_init_hw_data;
275         hwspecops->init_chip_data = pikronisa_init_chip_data;
276         hwspecops->init_obj_data = pikronisa_init_obj_data;
277         hwspecops->write_register = pikronisa_write_register;
278         hwspecops->read_register = pikronisa_read_register;
279         hwspecops->program_irq = pikronisa_program_irq;
280         return 0;
281 }