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