]> rtime.felk.cvut.cz Git - lincan.git/blob - lincan/src/pc_i03.c
c01e548b8ba031535190ffb5c387d626c31a4977
[lincan.git] / lincan / src / pc_i03.c
1 /* pc-i03.c
2  * Linux CAN-bus device driver.
3  * Written by Arnaud Westenberg email:arnaud@wnadoo.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/pc-i03.h"
14 #include "../include/sja1000.h"
15
16 /* Basic hardware io address. This is also stored in the hardware structure but
17  * we need it global, else we have to change many internal functions.
18  * pc-i03_base_addr is initialized in pc-i03_init_chip_data().
19  */
20 unsigned int pci03_base_addr; 
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 0x200  // The pc-i03 uses an additional 0x100 bytes reset space
28
29 /**
30  * pci03_request_io: - reserve io or memory range for can board
31  * @candev: pointer to candevice/board which asks for io. Field @io_addr
32  *      of @candev is used in most cases to define start of the range
33  *
34  * The function pci03_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/pc-i03.c
42  */
43 int pci03_request_io(struct candevice_t *candev)
44 {
45         if (!can_request_io_region(candev->io_addr,IO_RANGE,DEVICE_NAME)) {
46                 CANMSG("Unable to open port: 0x%lx\n",candev->io_addr);
47                 return -ENODEV;
48         } else {
49                 DEBUGMSG("Registered IO-memory: 0x%lx - 0x%lx\n", candev->io_addr, candev->io_addr + IO_RANGE - 1);
50         }
51         return 0;
52 }
53
54 /**
55  * pci03_elease_io - free reserved io memory range
56  * @candev: pointer to candevice/board which releases io
57  *
58  * The function pci03_release_io() is used to free reserved io-memory.
59  * In case you have reserved more io memory, don't forget to free it here.
60  * IO_RANGE is the io-memory range that gets released, please adjust according
61  * your hardware. Example: #define IO_RANGE 0x100 for i82527 chips or
62  * #define IO_RANGE 0x20 for sja1000 chips in basic CAN mode.
63  * Return Value: The function always returns zero
64  * File: src/pc-i03.c
65  */
66 int pci03_release_io(struct candevice_t *candev)
67 {
68         can_release_io_region(candev->io_addr,IO_RANGE);
69
70         return 0;
71 }
72
73 /**
74  * pci03_reset - hardware reset routine
75  * @candev: Pointer to candevice/board structure
76  *
77  * The function pci03_reset() is used to give a hardware reset. This is 
78  * rather hardware specific so I haven't included example code. Don't forget to 
79  * check the reset status of the chip before returning.
80  * Return Value: The function returns zero on success or %-ENODEV on failure
81  * File: src/pc-i03.c
82  */
83 int pci03_reset(struct candevice_t *candev)
84 {
85         int i=0;
86
87         DEBUGMSG("Resetting pc-i03 hardware ...\n");
88         pci03_write_register(0x01,pci03_base_addr +
89                                 0x100); // Write arbitrary data to reset mem
90         udelay(20000);
91
92         pci03_write_register(0x00, pci03_base_addr + SJACR);
93                                                                         
94         /* Check hardware reset status */
95         i=0;
96         while ( (pci03_read_register(pci03_base_addr + SJACR) & sjaCR_RR)
97                                                                  && (i<=15) ) {
98                 udelay(20000);
99                 i++;
100         }
101         if (i>=15) {
102                 CANMSG("Reset status timeout!\n");
103                 CANMSG("Please check your hardware.\n");
104                 return -ENODEV;
105         }
106         else
107                 DEBUGMSG("Chip[0] reset status ok.\n");
108
109         return 0;
110 }
111
112 #define RESET_ADDR 0x100
113 #define NR_82527 0
114 #define NR_SJA1000 1
115
116 /**
117  * pci03_init_hw_data - Initialize hardware cards
118  * @candev: Pointer to candevice/board structure
119  *
120  * The function pci03_init_hw_data() is used to initialize the hardware
121  * structure containing information about the installed CAN-board.
122  * %RESET_ADDR represents the io-address of the hardware reset register.
123  * %NR_82527 represents the number of intel 82527 chips on the board.
124  * %NR_SJA1000 represents the number of philips sja1000 chips on the board.
125  * The flags entry can currently only be %CANDEV_PROGRAMMABLE_IRQ to indicate that
126  * the hardware uses programmable interrupts.
127  * Return Value: The function always returns zero
128  * File: src/pc-i03.c
129  */
130 int pci03_init_hw_data(struct candevice_t *candev) 
131 {
132         candev->res_addr=RESET_ADDR;
133         candev->nr_82527_chips=NR_82527;
134         candev->nr_sja1000_chips=NR_SJA1000;
135         candev->nr_all_chips=NR_82527+NR_SJA1000;
136         return 0;
137 }
138
139 #define CHIP_TYPE "sja1000"
140 /**
141  * pci03_init_chip_data - Initialize chips
142  * @candev: Pointer to candevice/board structure
143  * @chipnr: Number of the CAN chip on the hardware card
144  *
145  * The function pci03_init_chip_data() is used to initialize the hardware
146  * structure containing information about the CAN chips.
147  * %CHIP_TYPE represents the type of CAN chip. %CHIP_TYPE can be "i82527" or
148  * "sja1000".
149  * The @chip_base_addr entry represents the start of the 'official' memory map
150  * of the installed chip. It's likely that this is the same as the @io_addr
151  * argument supplied at module loading time.
152  * The @clock entry holds the chip clock value in Hz.
153  * The entry @sja_cdr_reg holds hardware specific options for the Clock Divider
154  * register. Options defined in the %sja1000.h file:
155  * %sjaCDR_CLKOUT_MASK, %sjaCDR_CLK_OFF, %sjaCDR_RXINPEN, %sjaCDR_CBP, %sjaCDR_PELICAN
156  * The entry @sja_ocr_reg holds hardware specific options for the Output Control
157  * register. Options defined in the %sja1000.h file:
158  * %sjaOCR_MODE_BIPHASE, %sjaOCR_MODE_TEST, %sjaOCR_MODE_NORMAL, %sjaOCR_MODE_CLOCK,
159  * %sjaOCR_TX0_LH, %sjaOCR_TX1_ZZ.
160  * The entry @int_clk_reg holds hardware specific options for the Clock Out
161  * register. Options defined in the %i82527.h file:
162  * %iCLK_CD0, %iCLK_CD1, %iCLK_CD2, %iCLK_CD3, %iCLK_SL0, %iCLK_SL1.
163  * The entry @int_bus_reg holds hardware specific options for the Bus 
164  * Configuration register. Options defined in the %i82527.h file:
165  * %iBUS_DR0, %iBUS_DR1, %iBUS_DT1, %iBUS_POL, %iBUS_CBY.
166  * Return Value: The function always returns zero
167  * File: src/pc-i03.c
168  */
169 int pci03_init_chip_data(struct candevice_t *candev, int chipnr)
170 {
171         pci03_base_addr = candev->io_addr;
172         candev->chip[chipnr]->chip_type=CHIP_TYPE;
173         candev->chip[chipnr]->chip_base_addr=candev->io_addr;
174         candev->chip[chipnr]->clock = 16000000;
175         candev->chip[chipnr]->sja_cdr_reg = sjaCDR_CBP | sjaCDR_CLK_OFF;
176         candev->chip[chipnr]->sja_ocr_reg = sjaOCR_MODE_NORMAL | 
177                                                         sjaOCR_TX0_HL | sjaOCR_TX1_LZ;
178
179         return 0;
180 }
181
182 /**
183  * pci03_init_obj_data - Initialize message buffers
184  * @chip: Pointer to chip specific structure
185  * @objnr: Number of the message buffer
186  *
187  * The function pci03_init_obj_data() is used to initialize the hardware
188  * structure containing information about the different message objects on the
189  * CAN chip. In case of the sja1000 there's only one message object but on the
190  * i82527 chip there are 15.
191  * The code below is for a i82527 chip and initializes the object base addresses
192  * The entry @obj_base_addr represents the first memory address of the message 
193  * object. In case of the sja1000 @obj_base_addr is taken the same as the chips
194  * base address.
195  * Unless the hardware uses a segmented memory map, flags can be set zero.
196  * Return Value: The function always returns zero
197  * File: src/pc-i03.c
198  */
199 int pci03_init_obj_data(struct chip_t *chip, int objnr)
200 {
201         chip->msgobj[objnr]->obj_base_addr=chip->chip_base_addr;
202         
203         return 0;
204 }
205
206 /**
207  * pci03_program_irq - program interrupts
208  * @candev: Pointer to candevice/board structure
209  *
210  * The function pci03_program_irq() is used for hardware that uses 
211  * programmable interrupts. If your hardware doesn't use programmable interrupts
212  * you should not set the @candevices_t->flags entry to %CANDEV_PROGRAMMABLE_IRQ and 
213  * leave this function unedited. Again this function is hardware specific so 
214  * there's no example code.
215  * Return value: The function returns zero on success or %-ENODEV on failure
216  * File: src/pc-i03.c
217  */
218 int pci03_program_irq(struct candevice_t *candev)
219 {
220         return 0;
221 }
222
223 /**
224  * pci03_write_register - Low level write register routine
225  * @data: data to be written
226  * @address: memory address to write to
227  *
228  * The function pci03_write_register() is used to write to hardware registers
229  * on the CAN chip. You should only have to edit this function if your hardware
230  * uses some specific write process.
231  * Return Value: The function does not return a value
232  * File: src/pc-i03.c
233  */
234 void pci03_write_register(unsigned data, unsigned long address)
235 {
236         unsigned int *pci03_base_ptr;
237         unsigned short address_to_write;
238
239         /* The read/write functions are called by an extra abstract function.
240          * This extra function adds the basic io address of the card to the
241          * memory address we want to write to, so we substract the basic io
242          * address again to obtain the offset into the hardware's memory map.
243          */
244         address_to_write = address - pci03_base_addr; // Offset
245         pci03_base_ptr = (unsigned int *)(pci03_base_addr * 0x100001);
246         (*(pci03_base_ptr+address_to_write)) = data;
247 }
248
249 /**
250  * pci03_read_register - Low level read register routine
251  * @address: memory address to read from
252  *
253  * The function pci03_read_register() is used to read from hardware registers
254  * on the CAN chip. You should only have to edit this function if your hardware
255  * uses some specific read process.
256  * Return Value: The function returns the value stored in @address
257  * File: src/pc-i03.c
258  */
259 unsigned pci03_read_register(unsigned long address)
260 {
261         unsigned int *pci03_base_ptr;
262         unsigned short address_to_read;
263
264         /* The read/write functions are called by an extra abstract function.
265          * This extra function adds the basic io address of the card to the
266          * memory address we want to write to, so we substract the basic io
267          * address again to obtain the offset into the hardware's memory map.
268          */
269         address_to_read = address - pci03_base_addr;
270         pci03_base_ptr = (unsigned int *)(pci03_base_addr * 0x100001);
271         return (*(pci03_base_ptr+address_to_read));
272 }
273
274 int pci03_register(struct hwspecops_t *hwspecops)
275 {
276         hwspecops->request_io = pci03_request_io;
277         hwspecops->release_io = pci03_release_io;
278         hwspecops->reset = pci03_reset;
279         hwspecops->init_hw_data = pci03_init_hw_data;
280         hwspecops->init_chip_data = pci03_init_chip_data;
281         hwspecops->init_obj_data = pci03_init_obj_data;
282         hwspecops->write_register = pci03_write_register;
283         hwspecops->read_register = pci03_read_register;
284         hwspecops->program_irq = pci03_program_irq;
285         return 0;
286 }