]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/staging/gdm72xx/sdio_boot.c
Merge tag 'staging-3.6-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/gregkh...
[karo-tx-linux.git] / drivers / staging / gdm72xx / sdio_boot.c
1 /*
2  * Copyright (c) 2012 GCT Semiconductor, Inc. All rights reserved.
3  *
4  * This software is licensed under the terms of the GNU General Public
5  * License version 2, as published by the Free Software Foundation, and
6  * may be copied, distributed, and modified under those terms.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  */
13
14 #include <linux/module.h>
15 #include <linux/kernel.h>
16 #include <linux/init.h>
17 #include <linux/mm.h>
18 #include <linux/uaccess.h>
19 #include <linux/fs.h>
20 #include <linux/sched.h>
21 #include <linux/slab.h>
22
23 #include <linux/mmc/core.h>
24 #include <linux/mmc/card.h>
25 #include <linux/mmc/sdio_func.h>
26
27 #include "gdm_sdio.h"
28
29 #define TYPE_A_HEADER_SIZE      4
30 #define TYPE_A_LOOKAHEAD_SIZE   16
31 #define YMEM0_SIZE                      0x8000  /* 32kbytes */
32 #define DOWNLOAD_SIZE           (YMEM0_SIZE - TYPE_A_HEADER_SIZE)
33
34 #define KRN_PATH        "/lib/firmware/gdm72xx/gdmskrn.bin"
35 #define RFS_PATH        "/lib/firmware/gdm72xx/gdmsrfs.bin"
36
37 static u8 *tx_buf;
38
39 static int ack_ready(struct sdio_func *func)
40 {
41         unsigned long start = jiffies;
42         u8 val;
43         int ret;
44
45         while ((jiffies - start) < HZ) {
46                 val = sdio_readb(func, 0x13, &ret);
47                 if (val & 0x01)
48                         return 1;
49                 schedule();
50         }
51
52         return 0;
53 }
54
55 static int download_image(struct sdio_func *func, char *img_name)
56 {
57         int ret = 0, len, size, pno;
58         struct file *filp = NULL;
59         struct inode *inode = NULL;
60         u8 *buf = tx_buf;
61         loff_t pos = 0;
62
63         filp = filp_open(img_name, O_RDONLY | O_LARGEFILE, 0);
64         if (IS_ERR(filp)) {
65                 printk(KERN_ERR "Can't find %s.\n", img_name);
66                 return -ENOENT;
67         }
68
69         if (filp->f_dentry)
70                 inode = filp->f_dentry->d_inode;
71         if (!inode || !S_ISREG(inode->i_mode)) {
72                 printk(KERN_ERR "Invalid file type: %s\n", img_name);
73                 ret = -EINVAL;
74                 goto out;
75         }
76
77         size = i_size_read(inode->i_mapping->host);
78         if (size <= 0) {
79                 printk(KERN_ERR "Unable to find file size: %s\n", img_name);
80                 ret = size;
81                 goto out;
82         }
83
84         pno = 0;
85         while ((len = filp->f_op->read(filp, buf + TYPE_A_HEADER_SIZE,
86                                         DOWNLOAD_SIZE, &pos))) {
87                 if (len < 0) {
88                         ret = -1;
89                         goto out;
90                 }
91
92                 buf[0] = len & 0xff;
93                 buf[1] = (len >> 8) & 0xff;
94                 buf[2] = (len >> 16) & 0xff;
95
96                 if (pos >= size)        /* The last packet */
97                         buf[3] = 2;
98                 else
99                         buf[3] = 0;
100
101                 ret = sdio_memcpy_toio(func, 0, buf, len + TYPE_A_HEADER_SIZE);
102                 if (ret < 0) {
103                         printk(KERN_ERR "gdmwm: send image error: "
104                                 "packet number = %d ret = %d\n", pno, ret);
105                         goto out;
106                 }
107                 if (buf[3] == 2)        /* The last packet */
108                         break;
109                 if (!ack_ready(func)) {
110                         ret = -EIO;
111                         printk(KERN_ERR "gdmwm: Ack is not ready.\n");
112                         goto out;
113                 }
114                 ret = sdio_memcpy_fromio(func, buf, 0, TYPE_A_LOOKAHEAD_SIZE);
115                 if (ret < 0) {
116                         printk(KERN_ERR "gdmwm: receive ack error: "
117                                 "packet number = %d ret = %d\n", pno, ret);
118                         goto out;
119                 }
120                 sdio_writeb(func, 0x01, 0x13, &ret);
121                 sdio_writeb(func, 0x00, 0x10, &ret);    /* PCRRT */
122
123                 pno++;
124         }
125 out:
126         filp_close(filp, current->files);
127         return ret;
128 }
129
130 int sdio_boot(struct sdio_func *func)
131 {
132         static mm_segment_t fs;
133         int ret;
134
135         tx_buf = kmalloc(YMEM0_SIZE, GFP_KERNEL);
136         if (tx_buf == NULL) {
137                 printk(KERN_ERR "Error: kmalloc: %s %d\n", __func__, __LINE__);
138                 return -ENOMEM;
139         }
140
141         fs = get_fs();
142         set_fs(get_ds());
143
144         ret = download_image(func, KRN_PATH);
145         if (ret)
146                 goto restore_fs;
147         printk(KERN_INFO "GCT: Kernel download success.\n");
148
149         ret = download_image(func, RFS_PATH);
150         if (ret)
151                 goto restore_fs;
152         printk(KERN_INFO "GCT: Filesystem download success.\n");
153
154 restore_fs:
155         set_fs(fs);
156         kfree(tx_buf);
157         return ret;
158 }