]> rtime.felk.cvut.cz Git - lincan.git/blob - lincan/src/mpc5200.c
Actual driver code for directly mapped SJA1000 into PCI mem region 0.
[lincan.git] / lincan / src / mpc5200.c
1 /**************************************************************************/
2 /* File: mpc5200.c - Freescale MPC5200 MSCAN controller support           */
3 /*                                                                        */
4 /* LinCAN - (Not only) Linux CAN bus driver                               */
5 /* Copyright (C) 2002-2009 DCE FEE CTU Prague <http://dce.felk.cvut.cz>   */
6 /* Copyright (C) 2002-2009 Pavel Pisa <pisa@cmp.felk.cvut.cz>             */
7 /* Copyright (C) 2007-2008 Martin Petera <peterm4@fel.cvut.cz>            */
8 /* Funded by OCERA and FRESCOR IST projects                               */
9 /* Based on CAN driver code by Arnaud Westenberg <arnaud@wanadoo.nl>      */
10 /*                                                                        */
11 /* LinCAN is free software; you can redistribute it and/or modify it      */
12 /* under terms of the GNU General Public License as published by the      */
13 /* Free Software Foundation; either version 2, or (at your option) any    */
14 /* later version.  LinCAN is distributed in the hope that it will be      */
15 /* useful, but WITHOUT ANY WARRANTY; without even the implied warranty    */
16 /* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU    */
17 /* General Public License for more details. You should have received a    */
18 /* copy of the GNU General Public License along with LinCAN; see file     */
19 /* COPYING. If not, write to the Free Software Foundation, 675 Mass Ave,  */
20 /* Cambridge, MA 02139, USA.                                              */
21 /*                                                                        */
22 /* To allow use of LinCAN in the compact embedded systems firmware        */
23 /* and RT-executives (RTEMS for example), main authors agree with next    */
24 /* special exception:                                                     */
25 /*                                                                        */
26 /* Including LinCAN header files in a file, instantiating LinCAN generics */
27 /* or templates, or linking other files with LinCAN objects to produce    */
28 /* an application image/executable, does not by itself cause the          */
29 /* resulting application image/executable to be covered by                */
30 /* the GNU General Public License.                                        */
31 /* This exception does not however invalidate any other reasons           */
32 /* why the executable file might be covered by the GNU Public License.    */
33 /* Publication of enhanced or derived LinCAN files is required although.  */
34 /**************************************************************************/
35
36 #include "../include/can.h"
37 #include "../include/can_sysdep.h"
38 #include "../include/main.h"
39 #include "../include/mpc5200.h"
40 #include "../include/mscan.h"
41 #include "linux/of.h"
42 #include "linux/of_platform.h"
43
44 int mpc5200_init_device_node(struct canchip_t * chip, struct device_node * devnode);
45
46 /* in time when release_io should take place, we dont know the
47  * base addresses that has to be free.
48  * This variable keeps those addresses
49  */
50 unsigned long * chips_addr;
51
52
53 int mpc5200_request_io(struct candevice_t *candev)
54 {
55         int chipnr;
56         struct device_node * dn;
57         const char * of_devname = "can";
58
59         /* Workaround for OpenFormware */
60
61         /* Select device by chipnr:
62          *  first dev is @ 900
63          *  second dev is @ 980
64          *
65          * There are only two devices on MPC5200 */
66
67         /* initialize internal address storage */
68         chips_addr = (unsigned long*)kmalloc(candev->nr_all_chips * sizeof(unsigned long), GFP_KERNEL);
69
70         /* DEBUGMSG("Looking for device node '%s'\n", of_devname); */
71
72         chipnr = 0;
73         for_each_node_by_name(dn, of_devname)
74         {
75                 /* DEBUGMSG(" got OF node (%s)...\n", of_devname); */
76
77                 if (!of_device_is_available(dn))
78                 {
79                         DEBUGMSG("\tdevice not available\n");
80                         continue;
81                 }
82
83
84                 if (mpc5200_init_device_node(candev->chip[chipnr], dn))
85                         return -ENODEV;
86
87                 /* fill received address to internal storage */
88                 chips_addr[chipnr] = (unsigned long)candev->chip[chipnr]->chip_base_addr;
89
90                 /* original MIDAM Shark board use only one CAN chip - this handles similar cases */
91                 if (++chipnr >= candev->nr_all_chips)
92                         break;
93         }
94
95
96         DEBUGMSG("Succesfully mapped %d can devices\n", chipnr);
97         return 0;
98 }
99
100 int mpc5200_release_io(struct candevice_t *candev)
101 {
102         int i;
103
104         /*can_release_io_region(candev->io_addr, candev->nr_all_chips * IO_RANGE);*/
105
106         /* free all chips memorey space - using internal address storage */
107         for (i = 0; i < candev->nr_all_chips; i++)
108                 iounmap((void*)chips_addr[i]);
109
110         kfree(chips_addr);
111
112         return 0;
113 }
114
115 int mpc5200_reset(struct candevice_t *candev)
116 {
117         int i;
118         DEBUGMSG("Resetting MSCAN chips ...\n");
119
120         for (i = 0; i < candev->nr_all_chips; i++)
121         {
122             /* !!! Assuming this card has ONLY MSCAN chips !!! */
123             if (mscan_reset_chip(candev->chip[i]))
124                 return -ENODEV;
125         }
126
127         return 0;
128 }
129
130 int mpc5200_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_ALL;
136         /* candev->flags |= CANDEV_PROGRAMMABLE_IRQ; */
137
138         return 0;
139 }
140
141 /* special function for midam board */
142 int mpc5200_midam_init_hw_data(struct candevice_t *candev)
143 {
144         /* use same init routine */
145         mpc5200_init_hw_data(candev);
146
147         /* modify chip count */
148         candev->nr_all_chips = NR_ALL_MIDAM;
149
150         return 0;
151 }
152
153 int mpc5200_init_chip_data(struct candevice_t *candev, int chipnr)
154 {
155         mscan_fill_chipspecops(candev->chip[chipnr]);
156
157         candev->chip[chipnr]->clock = MPC5200_CLK_FREQ;
158         candev->chip[chipnr]->hostdevice = candev;
159
160 /*
161         candev->chip[chipnr]->chip_base_addr = can_ioport2ioptr(candev->io_addr) + chipnr * MPC5200_CAN_CHIP_OFFSET;  one chip with 2 interfaces
162         candev->chip[chipnr]->chip_irq = MPC5200_CAN_IRQ + chipnr;
163 */
164         DEBUGMSG("mpc5200_init_chip_data\n");
165
166         return 0;
167 }
168
169 int mpc5200_init_obj_data(struct canchip_t *chip, int objnr)
170 {
171         /* we have only two chips with only one mailbox each */
172         chip->msgobj[objnr]->obj_base_addr = (can_ioptr_t) MSCAN_CTL0 + MPC5200_CAN_CHIP_OFFSET * objnr;
173
174         return 0;
175 }
176
177 int mpc5200_program_irq(struct candevice_t *candev)
178 {
179         /* we  don't use programmable interrupt on MPC5200 */
180         return 0;
181 }
182
183
184 void mpc5200_write_register(unsigned data, can_ioptr_t address)
185 {
186         /* address is an absolute address */
187         /* DEBUGMSG("write register\n"); */
188         writeb(data, address);          /* regs in PowerPC (5200) are one-byte length */
189 }
190
191 unsigned mpc5200_read_register(can_ioptr_t address)
192 {
193         /* address is an absolute address */
194         /* DEBUGMSG("read register\n"); */
195         return readb(address);          /* regs in PowerPC (5200) are one-byte length */
196 }
197
198 int mpc5200_register(struct hwspecops_t *hwspecops)
199 {
200         hwspecops->request_io = mpc5200_request_io;
201         hwspecops->release_io = mpc5200_release_io;
202         hwspecops->reset = mpc5200_reset;
203         hwspecops->init_hw_data = mpc5200_init_hw_data;
204         hwspecops->init_chip_data = mpc5200_init_chip_data;
205         hwspecops->init_obj_data = mpc5200_init_obj_data;
206         hwspecops->program_irq = NULL; /* mpc5200_program_irq; */
207         hwspecops->write_register = mpc5200_write_register;
208         hwspecops->read_register = mpc5200_read_register;
209         return 0;
210 }
211
212 int mpc5200_midam_register(struct hwspecops_t *hwspecops)
213 {
214         /* use same register routine for MIDAM board */
215         mpc5200_register(hwspecops);
216
217         /* use specific init_hw_data */
218         hwspecops->init_hw_data = mpc5200_midam_init_hw_data;
219
220         return 0;
221 }
222
223 int mpc5200_init_device_node(struct canchip_t * chip, struct device_node * devnode)
224 {
225         chip->chip_base_addr = of_iomap(devnode, 0);
226         if (!chip->chip_base_addr)
227         {
228                 DEBUGMSG("\tcannot map IO - of_iomap\n");
229                 return -ENODEV;
230         }
231
232         chip->chip_irq = irq_of_parse_and_map(devnode, 0);
233         if (!chip->chip_irq)
234         {
235                 DEBUGMSG("\tcannot map IRQ\n");
236                 iounmap(chip->chip_base_addr);
237                 return -ENODEV;
238         }
239
240         DEBUGMSG("Bound to io-addr: 0x%08x IRQ: %d\n", (unsigned int)chip->chip_base_addr, chip->chip_irq);
241
242         return 0;
243 }