]> rtime.felk.cvut.cz Git - lincan.git/blob - lincan/src/tscan1.c
TS-CAN1 and TS-7KV support separated and updated. Needs testing.
[lincan.git] / lincan / src / tscan1.c
1 /* tscan1.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  * The support for TS-CAN1 and TS-7KV provided by Ronald Gomes
10  * from Technologic Systems <http://www.embeddedarm.com/>
11  */
12
13 #include "../include/can.h"
14 #include "../include/can_sysdep.h"
15 #include "../include/main.h"
16 #include "../include/sja1000p.h"
17
18 #include <linux/module.h>
19 #include "../include/modparms.h"
20 #include "../include/devcommon.h"
21
22 #include "../include/tscan1.h"
23
24 unsigned long tscanio[MAX_HW_CARDS]={-1,-1,-1,-1,-1,-1,-1,-1};
25 unsigned int tscanio_specified;
26
27 #if defined(TSXXX_BASE_IO)
28 unsigned long tsxxx_base=TSXXX_BASE_IO;
29 #else
30 unsigned long tsxxx_base=0;
31 #endif
32
33 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,12))
34 MODULE_PARM(tscanio, "1-" __MODULE_STRING(MAX_HW_CARDS)"i");
35 MODULE_PARM(tsxxx_base, "1i");
36 #else /* LINUX_VERSION_CODE >= 2,6,12 */
37 module_param_array(tscanio, ulong, &tscanio_specified, 0);
38 module_param(tsxxx_base, ulong, 0);
39 #endif /* LINUX_VERSION_CODE >= 2,6,12 */
40
41 MODULE_PARM_DESC(tscanio,"TSCAN CAN controller IO address for each board");
42 MODULE_PARM_DESC(tsxxx_base,"The base of the ISA/8-bit IO space for TSxxx CAN peripherals in the system");
43
44 static unsigned long tscan1_tscanio_ranges[] =
45                 { 0x100, 0x120, 0x180, 0x1A0, 0x200, 0x240, 0x280, 0x320};
46
47 static inline unsigned tscan1_read_pld_register(struct candevice_t *candev, can_ioptr_t address)
48 {
49         return candev->hwspecops->read_register(address);
50 }
51
52 static inline void tscan1_write_pld_register(struct candevice_t *candev, unsigned data, can_ioptr_t address)
53 {
54         candev->hwspecops->write_register(data, address);
55 }
56
57 /**
58  * tscan1_request_io: - reserve io or memory range for can board
59  * @candev: pointer to candevice/board which asks for io. Field @io_addr
60  *      of @candev is used in most cases to define start of the range
61  *
62  * The function tscan1_request_io() is used to reserve the io-memory. If your
63  * hardware uses a dedicated memory range as hardware control registers you
64  * will have to add the code to reserve this memory as well.
65  * %IO_RANGE is the io-memory range that gets reserved, please adjust according
66  * your hardware. Example: #define IO_RANGE 0x100 for i82527 chips or
67  * #define IO_RANGE 0x20 for sja1000 chips in basic CAN mode.
68  * Return Value: The function returns zero on success or %-ENODEV on failure
69  * File: src/tscan1.c
70  */
71 int tscan1_request_io(struct candevice_t *candev)
72 {
73         unsigned long can_io_addr;
74         unsigned long rebased_can_io_addr;
75         can_ioptr_t remap_can_io_addr;
76         can_ioptr_t remap_pld_io_addr;
77         unsigned char mode;
78         int i, j;
79
80
81         if (!can_request_io_region(candev->io_addr, TSCAN1_IO_RANGE, "tscan1-base")) {
82                 CANMSG("Unable to request base IO port: 0x%lx\n", candev->io_addr);
83                 return -ENODEV;
84         } else {
85                 DEBUGMSG("Registered base IO port: 0x%lx - 0x%lx\n",
86                         candev->io_addr, candev->io_addr+TSCAN1_IO_RANGE-1);
87         }
88
89         can_io_addr = tscanio[candev->candev_idx];
90
91         if(can_io_addr && (can_io_addr != (unsigned long)-1)) {
92                 rebased_can_io_addr = tsxxx_base + can_io_addr;
93
94                 if (!can_request_io_region(rebased_can_io_addr, TSCAN1_CAN_RANGE, "tscan1-can")) {
95                         CANMSG("Unable to request CAN IO port: 0x%lx\n", rebased_can_io_addr);
96                         can_release_io_region(candev->io_addr, TSCAN1_IO_RANGE);
97                         return -ENODEV;
98                 } else {
99                         DEBUGMSG("Registered CAN IO port: 0x%lx - 0x%lx\n",
100                         rebased_can_io_addr, rebased_can_io_addr+TSCAN1_CAN_RANGE-1);
101                 }
102         } else {
103                 for(i = 0; 1; i++) {
104
105                         if(i>=8) {
106                                 CANMSG("Unable find range for CAN IO port\n");
107                                 can_release_io_region(candev->io_addr, TSCAN1_IO_RANGE);
108                                 return -ENODEV;
109                         }
110
111                         can_io_addr = tscan1_tscanio_ranges[i];
112                         for (j = 0; j < MAX_HW_CARDS; j++) {
113                                 if(tscanio[j] == can_io_addr) {
114                                         j = -1;
115                                         break;
116                                 }
117                         }
118                         if(j<0)
119                                 continue;
120
121                         rebased_can_io_addr = tsxxx_base + can_io_addr;
122
123                         if (can_request_io_region(rebased_can_io_addr, TSCAN1_CAN_RANGE, "tscan1-can"))
124                                 break;
125                 }
126
127                 tscanio[candev->candev_idx] = can_io_addr;
128
129                 DEBUGMSG("Found free range and registered CAN IO port: 0x%lx - 0x%lx\n",
130                         rebased_can_io_addr, rebased_can_io_addr+TSCAN1_CAN_RANGE-1);
131         }
132
133         remap_can_io_addr = can_ulong2ioptr(rebased_can_io_addr);
134
135         candev->chip[0]->chip_base_addr = remap_can_io_addr;
136         candev->chip[0]->msgobj[0]->obj_base_addr = remap_can_io_addr;
137
138         mode = 0x60;
139         for(i = 0; i < 8; i++) {
140                 if (can_io_addr == tscan1_tscanio_ranges[i]) {
141                         mode |= i;
142                         break;
143                 }
144         }
145
146         remap_pld_io_addr = can_ulong2ioptr(candev->io_addr);
147
148         tscan1_write_pld_register(candev, 0x00, remap_pld_io_addr+TSCAN1_WIN_REG);
149         tscan1_write_pld_register(candev, mode, remap_pld_io_addr+TSCAN1_MOD_REG);
150
151         return 0;
152 }
153
154 /**
155  * tscan1_release_io - free reserved io memory range
156  * @candev: pointer to candevice/board which releases io
157  *
158  * The function tscan1_release_io() is used to free reserved io-memory.
159  * In case you have reserved more io memory, don't forget to free it here.
160  * IO_RANGE is the io-memory range that gets released, please adjust according
161  * your hardware. Example: #define IO_RANGE 0x100 for i82527 chips or
162  * #define IO_RANGE 0x20 for sja1000 chips in basic CAN mode.
163  * Return Value: The function always returns zero
164  * File: src/tscan1.c
165  */
166 int tscan1_release_io(struct candevice_t *candev)
167 {
168         unsigned long rebased_can_io_addr;
169         can_ioptr_t remap_pld_io_addr = can_ulong2ioptr(candev->io_addr);
170
171         if(candev->chip[0]){
172                 rebased_can_io_addr = candev->chip[0]->chip_base_addr;
173                 if(rebased_can_io_addr != (unsigned long)-1)
174                         can_release_io_region(rebased_can_io_addr, TSCAN1_CAN_RANGE);
175         }
176
177         tscan1_write_pld_register(candev, 0x20, remap_pld_io_addr+TSCAN1_MOD_REG);
178
179         can_release_io_region(candev->io_addr, TSCAN1_IO_RANGE);
180         return 0;
181 }
182
183 /**
184  * tscan1_reset - hardware reset routine
185  * @candev: Pointer to candevice/board structure
186  *
187  * The function tscan1_reset() is used to give a hardware reset. This is
188  * rather hardware specific so I haven't included example code. Don't forget to
189  * check the reset status of the chip before returning.
190  * Return Value: The function returns zero on success or %-ENODEV on failure
191  * File: src/tscan1.c
192  */
193 int tscan1_reset(struct candevice_t *candev)
194 {
195         unsigned short i=0, chipnr;
196         struct canchip_t *chip;
197
198         DEBUGMSG("Resetting %s hardware ...\n", candev->hwname);
199
200         for(chipnr=0;chipnr<candev->nr_sja1000_chips;chipnr++) {
201                 chip=candev->chip[chipnr];
202                 can_write_reg(chip, sjaMOD_RM, SJAMOD);
203                 udelay(1000);
204                 can_write_reg(chip, 0x00, SJAIER);
205                 udelay(1000);
206                 i=20;
207                 while (can_read_reg(chip, SJAMOD)&sjaMOD_RM){
208                         if(!i--) return -ENODEV;
209                         udelay(1000);
210                         can_write_reg(chip, 0, SJAMOD);
211                 }
212                 udelay(1000);
213                 can_write_reg(chip, sjaCDR_PELICAN, SJACDR);
214                 can_write_reg(chip, 0x00, SJAIER);
215         }
216
217         return 0;
218 }
219
220 #define RESET_ADDR 0x100
221 #define NR_82527 0
222 #define NR_SJA1000 1
223
224 int tscan1_check_presence(struct candevice_t *candev, unsigned long rebased_pld_io_addr, int *pjmp)
225 {
226         can_ioptr_t remap_pld_io_addr;
227         int result = -ENODEV;
228
229         if (!can_request_io_region(rebased_pld_io_addr, TSCAN1_IO_RANGE, "tscan1-probe"))
230                 return -ENODEV;
231
232         do {
233                 remap_pld_io_addr = can_ulong2ioptr(rebased_pld_io_addr);
234
235                 if (tscan1_read_pld_register(candev, remap_pld_io_addr+TSCAN1_ID0_REG)!=TSCAN1_ID0 ||
236                         tscan1_read_pld_register(candev, remap_pld_io_addr+TSCAN1_ID1_REG)!=TSCAN1_ID1)
237                         break;
238
239                 tscan1_write_pld_register(candev, 0x00, remap_pld_io_addr+TSCAN1_WIN_REG);
240                 tscan1_write_pld_register(candev, 0x20, remap_pld_io_addr+TSCAN1_MOD_REG);
241
242                 if(pjmp)
243                         *pjmp = tscan1_read_pld_register(candev, remap_pld_io_addr+TSCAN1_JMP_REG);
244
245                 result = 0;
246         } while (0);
247
248         can_release_io_region(rebased_pld_io_addr, TSCAN1_IO_RANGE);
249
250         return result;
251 }
252
253
254 /**
255  * tscan1_init_hw_data - Initialize hardware cards
256  * @candev: Pointer to candevice/board structure
257  *
258  * The function tscan1_init_hw_data() is used to initialize the hardware
259  * structure containing information about the installed CAN-board.
260  * %RESET_ADDR represents the io-address of the hardware reset register.
261  * %NR_82527 represents the number of intel 82527 chips on the board.
262  * %NR_SJA1000 represents the number of philips sja1000 chips on the board.
263  * The flags entry can currently only be %CANDEV_PROGRAMMABLE_IRQ to indicate that
264  * the hardware uses programmable interrupts.
265  * Return Value: The function always returns zero
266  * File: src/tscan1.c
267  */
268
269 int tscan1_init_hw_data(struct candevice_t *candev)
270 {
271         int i, j, jmp;
272         unsigned long io_addr;
273         unsigned long rebased_pld_io_addr;
274
275         io_addr = candev->io_addr;
276
277         if(io_addr && (io_addr != (unsigned long)-1)) {
278                 rebased_pld_io_addr = io_addr + tsxxx_base;
279
280                 if(tscan1_check_presence(candev, rebased_pld_io_addr, &jmp)){
281                         CANMSG("No TSCAN1 card found at address 0x%lx\n",
282                                 rebased_pld_io_addr);
283                         return -ENODEV;
284                 }
285         } else {
286                 DEBUGMSG("Scanning bus for TS-CAN1 boards...\n");
287
288                 for (i=0; 1;i++)
289                 {
290                         if(i >= 4) {
291                                 CANMSG("No TS-CAN1 boards found for slot %d\n", candev->candev_idx);
292                                 return -ENODEV;
293                         }
294
295                         io_addr = TSCAN1_BASE_IO + i*TSCAN1_IO_RANGE;
296                         rebased_pld_io_addr = io_addr + tsxxx_base;
297
298                         for (j = 0; j < MAX_HW_CARDS; j++) {
299                                 if(io[j] == io_addr){
300                                         j = -1;
301                                         break;
302                                 }
303                         }
304                         if(j<0)
305                                 continue;
306
307                         if(!tscan1_check_presence(candev, rebased_pld_io_addr, &jmp))
308                                 break;
309
310                 }
311                 DEBUGMSG("TS-CAN1 board was found at 0x%lx for driver slot %d\n",
312                                         io_addr, candev->candev_idx);
313
314                 io[candev->candev_idx] = io_addr;
315         }
316
317         candev->io_addr = rebased_pld_io_addr;
318         /* unused reset address is used to store jumper setting */
319         candev->res_addr = jmp;
320
321         candev->nr_82527_chips=NR_82527;
322         candev->nr_sja1000_chips=NR_SJA1000;
323         candev->nr_all_chips=NR_82527+NR_SJA1000;
324         candev->flags &= ~CANDEV_PROGRAMMABLE_IRQ;
325
326         DEBUGMSG("Memory region at 0x%lx assigned to sja1000 of driver %d/%s\n",
327                 candev->io_addr, candev->candev_idx, candev->hwname);
328
329         return 0;
330 }
331
332
333 /**
334  * tscan1_init_chip_data - Initialize chips
335  * @candev: Pointer to candevice/board structure
336  * @chipnr: Number of the CAN chip on the hardware card
337  *
338  * The function tscan1_init_chip_data() is used to initialize the hardware
339  * structure containing information about the CAN chips.
340  * %CHIP_TYPE represents the type of CAN chip. %CHIP_TYPE can be "i82527" or
341  * "sja1000".
342  * The @chip_base_addr entry represents the start of the 'official' memory map
343  * of the installed chip. It's likely that this is the same as the @io_addr
344  * argument supplied at module loading time.
345  * The @clock entry holds the chip clock value in Hz.
346  * The entry @sja_cdr_reg holds hardware specific options for the Clock Divider
347  * register. Options defined in the %sja1000.h file:
348  * %sjaCDR_CLKOUT_MASK, %sjaCDR_CLK_OFF, %sjaCDR_RXINPEN, %sjaCDR_CBP, %sjaCDR_PELICAN
349  * The entry @sja_ocr_reg holds hardware specific options for the Output Control
350  * register. Options defined in the %sja1000.h file:
351  * %sjaOCR_MODE_BIPHASE, %sjaOCR_MODE_TEST, %sjaOCR_MODE_NORMAL, %sjaOCR_MODE_CLOCK,
352  * %sjaOCR_TX0_LH, %sjaOCR_TX1_ZZ.
353  * The entry @int_clk_reg holds hardware specific options for the Clock Out
354  * register. Options defined in the %i82527.h file:
355  * %iCLK_CD0, %iCLK_CD1, %iCLK_CD2, %iCLK_CD3, %iCLK_SL0, %iCLK_SL1.
356  * The entry @int_bus_reg holds hardware specific options for the Bus
357  * Configuration register. Options defined in the %i82527.h file:
358  * %iBUS_DR0, %iBUS_DR1, %iBUS_DT1, %iBUS_POL, %iBUS_CBY.
359  * Return Value: The function always returns zero
360  * File: src/tscan1.c
361  */
362 int tscan1_init_chip_data(struct candevice_t *candev, int chipnr)
363 {
364         unsigned long default_clk = 16000 * 1000;
365         int jmp;
366         int irq = -1;
367
368         /* unused reset address is used to store jumper setting */
369         jmp = candev->res_addr;
370
371         if (jmp&0x10 && jmp&0x20) irq=TSXXX_IRQ5;
372         else if (jmp&0x10) irq=TSXXX_IRQ6;
373         else if (jmp&0x20) irq=TSXXX_IRQ7;
374
375         if(irq<0) {
376                 CANMSG("Jumpers select no IRQ for TSCAN1 at 0x%lx of driver %d/%s\n",
377                         candev->io_addr, candev->candev_idx, candev->hwname);
378                 return -ENODEV;
379         }
380         candev->chip[chipnr]->chip_irq = irq;
381
382         sja1000p_fill_chipspecops(candev->chip[chipnr]);
383
384         if(candev->chip[chipnr]->clock <= 0)
385                 candev->chip[chipnr]->clock = default_clk;
386         candev->chip[chipnr]->int_clk_reg = 0x0;
387         candev->chip[chipnr]->int_bus_reg = 0x0;
388         candev->chip[chipnr]->sja_cdr_reg = sjaCDR_CBP | sjaCDR_CLK_OFF;
389         candev->chip[chipnr]->sja_ocr_reg = sjaOCR_MODE_NORMAL | sjaOCR_TX0_LH;
390         /* 
391          * The address is assigned during tscan1_request_io()
392          * according to found free ranges or tscanio option
393          */
394         candev->chip[chipnr]->chip_base_addr = (unsigned long)-1;
395
396         return 0;
397 }
398
399 /**
400  * tscan1_init_obj_data - Initialize message buffers
401  * @chip: Pointer to chip specific structure
402  * @objnr: Number of the message buffer
403  *
404  * The function tscan1_init_obj_data() is used to initialize the hardware
405  * structure containing information about the different message objects on the
406  * CAN chip. In case of the sja1000 there's only one message object but on the
407  * i82527 chip there are 15.
408  * The code below is for a i82527 chip and initializes the object base addresses
409  * The entry @obj_base_addr represents the first memory address of the message
410  * object. In case of the sja1000 @obj_base_addr is taken the same as the chips
411  * base address.
412  * Unless the hardware uses a segmented memory map, flags can be set zero.
413  * Return Value: The function always returns zero
414  * File: src/tscan1.c
415  */
416 int tscan1_init_obj_data(struct canchip_t *chip, int objnr)
417 {
418         chip->msgobj[objnr]->obj_base_addr = chip->chip_base_addr;
419         return 0;
420 }
421
422 /**
423  * tscan1_program_irq - program interrupts
424  * @candev: Pointer to candevice/board structure
425  *
426  * The function tscan1_program_irq() is used for hardware that uses
427  * programmable interrupts. If your hardware doesn't use programmable interrupts
428  * you should not set the @candevices_t->flags entry to %CANDEV_PROGRAMMABLE_IRQ and
429  * leave this function unedited. Again this function is hardware specific so
430  * there's no example code.
431  * Return value: The function returns zero on success or %-ENODEV on failure
432  * File: src/tscan1.c
433  */
434 int tscan1_program_irq(struct candevice_t *candev)
435 {
436         return 0;
437 }
438
439 /**
440  * tscan1_write_register - Low level write register routine
441  * @data: data to be written
442  * @address: memory address to write to
443  *
444  * The function tscan1_write_register() is used to write to hardware registers
445  * on the CAN chip. You should only have to edit this function if your hardware
446  * uses some specific write process.
447  * Return Value: The function does not return a value
448  * File: src/tscan1.c
449  */
450 void tscan1_write_register(unsigned data, can_ioptr_t address)
451 {
452         can_outb(data, address);
453 }
454
455 void tscan1mmio_write_register(unsigned data, can_ioptr_t address)
456 {
457         can_writeb(data, address);
458 }
459
460
461 /**
462  * tscan1_read_register - Low level read register routine
463  * @address: memory address to read from
464  *
465  * The function tscan1_read_register() is used to read from hardware registers
466  * on the CAN chip. You should only have to edit this function if your hardware
467  * uses some specific read process.
468  * Return Value: The function returns the value stored in @address
469  * File: src/tscan1.c
470  */
471 unsigned tscan1_read_register(can_ioptr_t address)
472 {
473         return can_inb(address);
474 }
475
476 unsigned tscan1mmio_read_register(can_ioptr_t address)
477 {
478         return can_readb(address);
479 }
480
481 int tscan1_register(struct hwspecops_t *hwspecops)
482 {
483         hwspecops->request_io = tscan1_request_io;
484         hwspecops->release_io = tscan1_release_io;
485         hwspecops->reset = tscan1_reset;
486         hwspecops->init_hw_data = tscan1_init_hw_data;
487         hwspecops->init_chip_data = tscan1_init_chip_data;
488         hwspecops->init_obj_data = tscan1_init_obj_data;
489         hwspecops->write_register = tscan1_write_register;
490         hwspecops->read_register = tscan1_read_register;
491         hwspecops->program_irq = tscan1_program_irq;
492         return 0;
493 }
494
495 int tscan1mmio_register(struct hwspecops_t *hwspecops)
496 {
497         hwspecops->request_io = tscan1_request_io;
498         hwspecops->release_io = tscan1_release_io;
499         hwspecops->reset = tscan1_reset;
500         hwspecops->init_hw_data = tscan1_init_hw_data;
501         hwspecops->init_chip_data = tscan1_init_chip_data;
502         hwspecops->init_obj_data = tscan1_init_obj_data;
503         hwspecops->write_register = tscan1mmio_write_register;
504         hwspecops->read_register = tscan1mmio_read_register;
505         hwspecops->program_irq = tscan1_program_irq;
506         return 0;
507 }