]> rtime.felk.cvut.cz Git - lincan.git/blob - lincan/src/nsi.c
Included support for PiMX1 board with SJA1000 on MX_DIS1 expansion board.
[lincan.git] / lincan / src / nsi.c
1 /* nsi.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/nsi.h"
14 #include "../include/i82527.h"
15
16 int nsican_irq=-1;
17 unsigned long nsican_base=0x0;
18
19 static can_spinlock_t nsican_port_lock=SPIN_LOCK_UNLOCKED;
20
21 /* IO_RANGE is the io-memory range that gets reserved, please adjust according
22  * your hardware. Example: #define IO_RANGE 0x100 for i82527 chips or
23  * #define IO_RANGE 0x20 for sja1000 chips.
24  */
25 #define IO_RANGE 0x04
26
27 /* The function template_request_io is used to reserve the io-memory. If your
28  * hardware uses a dedicated memory range as hardware control registers you
29  * will have to add the code to reserve this memory as well.
30  * The reserved memory starts at candev->io_addr, wich is the module parameter io.
31  */
32 int nsi_request_io(struct candevice_t *candev)
33 {
34
35         if (!can_request_io_region(candev->io_addr,IO_RANGE,DEVICE_NAME)) {
36                 CANMSG("Unable to open port: 0x%lx\n",candev->io_addr);
37                 return -ENODEV;
38         } else {
39                 DEBUGMSG("Registered IO-memory: 0x%lx - 0x%lx\n", candev->io_addr, 
40                          candev->io_addr + IO_RANGE - 1);
41         }
42         return 0;
43 }
44
45 /* The function template_release_io is used to free the previously reserved 
46  * io-memory. In case you reserved more memory, don't forget to free it here.
47  */
48 int nsi_release_io(struct candevice_t *candev)
49 {
50
51         can_release_io_region(candev->io_addr,IO_RANGE);
52
53         return 0;
54 }
55
56 /* The function template_reset is used to give a hardware reset. This is rather
57  * hardware specific so I haven't included example code. Don't forget to check
58  * the reset status of the chip before returning.
59  */
60 int nsi_reset(struct candevice_t *candev)
61 {
62     int i; 
63
64     DEBUGMSG("Resetting nsi hardware ...\n");
65     /* we don't use template_write_register because we don't use the two first
66        register of the card but the third in order to make a hard reset */
67     outb (1, nsican_base + candev->res_addr);
68     outb (0, nsican_base + candev->res_addr);
69     for (i = 1; i < 1000; i++)
70         udelay (1000);
71     
72     
73     /* Check hardware reset status */ 
74     i=0;
75     while ( (nsi_read_register(nsican_base+iCPU) & iCPU_RST) && (i<=15)) {
76         udelay(20000);
77         i++;
78     }
79     if (i>=15) {
80         CANMSG("Reset status timeout!\n");
81         CANMSG("Please check your hardware.\n");
82         return -ENODEV;
83     }
84     else
85         DEBUGMSG("Chip0 reset status ok.\n");
86
87     return 0;
88 }
89
90 /* The function template_init_hw_data is used to initialize the hardware
91  * structure containing information about the installed CAN-board.
92  * RESET_ADDR represents the io-address of the hardware reset register.
93  * NR_82527 represents the number of intel 82527 chips on the board.
94  * NR_SJA1000 represents the number of philips sja1000 chips on the board.
95  * The flags entry can currently only be CANDEV_PROGRAMMABLE_IRQ to indicate that
96  * the hardware uses programmable interrupts.
97  */
98 #define RESET_ADDR 0x02
99 #define NR_82527 1
100 #define NR_SJA1000 0
101
102 int nsi_init_hw_data(struct candevice_t *candev) 
103      {
104         candev->res_addr=RESET_ADDR;
105         candev->nr_82527_chips=1;
106         candev->nr_sja1000_chips=0;
107         candev->nr_all_chips=1;
108         candev->flags |= CANDEV_PROGRAMMABLE_IRQ;
109
110         return 0;
111 }
112
113 /* The function template_init_chip_data is used to initialize the hardware
114  * structure containing information about the CAN chips.
115  * CHIP_TYPE represents the type of CAN chip. CHIP_TYPE can be "i82527" or
116  * "sja1000".
117  * The chip_base_addr entry represents the start of the 'official' memory map
118  * of the installed chip. It's likely that this is the same as the candev->io_addr
119  * argument supplied at module loading time.
120  * The clock argument holds the chip clock value in Hz.
121  */
122
123 int nsi_init_chip_data(struct candevice_t *candev, int chipnr)
124 {
125         i82527_fill_chipspecops(candev->chip[chipnr]);
126         candev->chip[chipnr]->chip_base_addr=
127             candev->io_addr;
128         candev->chip[chipnr]->clock = 16000000;
129         nsican_irq=candev->chip[chipnr]->chip_irq;      
130         nsican_base=candev->chip[chipnr]->chip_base_addr;
131         candev->chip[chipnr]->int_cpu_reg = iCPU_DSC;
132         candev->chip[chipnr]->int_clk_reg = iCLK_SL1;
133         candev->chip[chipnr]->int_bus_reg = iBUS_CBY;
134
135         return 0;
136 }
137
138  /* The function template_init_obj_data is used to initialize the hardware
139  * structure containing information about the different message objects on the
140  * CAN chip. In case of the sja1000 there's only one message object but on the
141  * i82527 chip there are 15.
142  * The code below is for a i82527 chip and initializes the object base addresses
143  * The entry obj_base_addr represents the first memory address of the message 
144  * object. In case of the sja1000 obj_base_addr is taken the same as the chips
145  * base address.
146  * Unless the hardware uses a segmented memory map, flags can be set zero.
147  */
148 int nsi_init_obj_data(struct canchip_t *chip, int objnr)
149 {
150
151         chip->msgobj[objnr]->obj_base_addr=
152             chip->chip_base_addr+(objnr+1)*0x10;
153         
154         return 0;
155 }
156
157 /* The function template_program_irq is used for hardware that uses programmable
158  * interrupts. If your hardware doesn't use programmable interrupts you should
159  * not set the candevices_t->flags entry to CANDEV_PROGRAMMABLE_IRQ and leave this
160  * function unedited. Again this function is hardware specific so there's no
161  * example code.
162  */
163 int nsi_program_irq(struct candevice_t *candev)
164 {
165         return 0;
166 }
167
168 /* The function template_write_register is used to write to hardware registers
169  * on the CAN chip. You should only have to edit this function if your hardware
170  * uses some specific write process.
171  */
172 void nsi_write_register(unsigned data, unsigned long address)
173 {
174     /* address is an absolute address */
175
176     /* the nsi card has two registers, the address register at 0x0
177        and the data register at 0x01 */
178
179     /* write the relative address on the eight LSB bits 
180        and the data on the eight MSB bits in one time */
181     outw(address-nsican_base + (256 * data), nsican_base); 
182 }
183
184 /* The function template_read_register is used to read from hardware registers
185  * on the CAN chip. You should only have to edit this function if your hardware
186  * uses some specific read process.
187  */
188 unsigned nsi_read_register(unsigned long address)
189 {
190     /* this is the same thing that the function write_register.
191        We use the two register, we write the address where we 
192        want to read in a first time. In a second time we read the
193        data */
194         unsigned char ret;
195         can_spin_irqflags_t flags;
196     
197         can_spin_lock_irqsave(&nsican_port_lock,flags);
198         outb(address-nsican_base, nsican_base);
199         ret=inb(nsican_base+1);
200         can_spin_unlock_irqrestore(&nsican_port_lock,flags);
201         return ret;
202 }
203
204
205  /* !!! Don't change this function !!! */
206 int nsi_register(struct hwspecops_t *hwspecops)
207 {
208         hwspecops->request_io = nsi_request_io;
209         hwspecops->release_io = nsi_release_io;
210         hwspecops->reset = nsi_reset;
211         hwspecops->init_hw_data = nsi_init_hw_data;
212         hwspecops->init_chip_data = nsi_init_chip_data;
213         hwspecops->init_obj_data = nsi_init_obj_data;
214         hwspecops->write_register = nsi_write_register;
215         hwspecops->read_register = nsi_read_register;
216         hwspecops->program_irq = nsi_program_irq;
217         return 0;
218 }