Linux-libre 3.10.54-gnu
[librecmc/linux-libre.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 <linux/firmware.h>
28
29 #include "gdm_sdio.h"
30 #include "sdio_boot.h"
31
32 #define TYPE_A_HEADER_SIZE      4
33 #define TYPE_A_LOOKAHEAD_SIZE   16
34 #define YMEM0_SIZE              0x8000  /* 32kbytes */
35 #define DOWNLOAD_SIZE           (YMEM0_SIZE - TYPE_A_HEADER_SIZE)
36
37 #define FW_DIR                  "gdm72xx/"
38 #define FW_KRN                  "/*(DEBLOBBED)*/"
39 #define FW_RFS                  "/*(DEBLOBBED)*/"
40
41 static u8 *tx_buf;
42
43 static int ack_ready(struct sdio_func *func)
44 {
45         unsigned long start = jiffies;
46         u8 val;
47         int ret;
48
49         while ((jiffies - start) < HZ) {
50                 val = sdio_readb(func, 0x13, &ret);
51                 if (val & 0x01)
52                         return 1;
53                 schedule();
54         }
55
56         return 0;
57 }
58
59 static int download_image(struct sdio_func *func, const char *img_name)
60 {
61         int ret = 0, len, pno;
62         u8 *buf = tx_buf;
63         loff_t pos = 0;
64         int img_len;
65         const struct firmware *firm;
66
67         ret = reject_firmware(&firm, img_name, &func->dev);
68         if (ret < 0) {
69                 dev_err(&func->dev,
70                         "requesting firmware %s failed with error %d\n",
71                         img_name, ret);
72                 return ret;
73         }
74
75         buf = kmalloc(DOWNLOAD_SIZE + TYPE_A_HEADER_SIZE, GFP_KERNEL);
76         if (buf == NULL)
77                 return -ENOMEM;
78
79         img_len = firm->size;
80
81         if (img_len <= 0) {
82                 ret = -1;
83                 goto out;
84         }
85
86         pno = 0;
87         while (img_len > 0) {
88                 if (img_len > DOWNLOAD_SIZE) {
89                         len = DOWNLOAD_SIZE;
90                         buf[3] = 0;
91                 } else {
92                         len = img_len; /* the last packet */
93                         buf[3] = 2;
94                 }
95
96                 buf[0] = len & 0xff;
97                 buf[1] = (len >> 8) & 0xff;
98                 buf[2] = (len >> 16) & 0xff;
99
100                 memcpy(buf+TYPE_A_HEADER_SIZE, firm->data + pos, len);
101                 ret = sdio_memcpy_toio(func, 0, buf, len + TYPE_A_HEADER_SIZE);
102                 if (ret < 0) {
103                         dev_err(&func->dev,
104                                 "send image error: packet number = %d ret = %d\n",
105                                 pno, ret);
106                         goto out;
107                 }
108
109                 if (buf[3] == 2)        /* The last packet */
110                         break;
111                 if (!ack_ready(func)) {
112                         ret = -EIO;
113                         dev_err(&func->dev, "Ack is not ready.\n");
114                         goto out;
115                 }
116                 ret = sdio_memcpy_fromio(func, buf, 0, TYPE_A_LOOKAHEAD_SIZE);
117                 if (ret < 0) {
118                         dev_err(&func->dev,
119                                 "receive ack error: packet number = %d ret = %d\n",
120                                 pno, ret);
121                         goto out;
122                 }
123                 sdio_writeb(func, 0x01, 0x13, &ret);
124                 sdio_writeb(func, 0x00, 0x10, &ret);    /* PCRRT */
125
126                 img_len -= DOWNLOAD_SIZE;
127                 pos += DOWNLOAD_SIZE;
128                 pno++;
129         }
130
131 out:
132         kfree(buf);
133         return ret;
134 }
135
136 int sdio_boot(struct sdio_func *func)
137 {
138         int ret;
139         const char *krn_name = FW_DIR FW_KRN;
140         const char *rfs_name = FW_DIR FW_RFS;
141
142         tx_buf = kmalloc(YMEM0_SIZE, GFP_KERNEL);
143         if (tx_buf == NULL)
144                 return -ENOMEM;
145
146         ret = download_image(func, krn_name);
147         if (ret)
148                 goto restore_fs;
149         dev_info(&func->dev, "GCT: Kernel download success.\n");
150
151         ret = download_image(func, rfs_name);
152         if (ret)
153                 goto restore_fs;
154         dev_info(&func->dev, "GCT: Filesystem download success.\n");
155
156 restore_fs:
157         kfree(tx_buf);
158         return ret;
159 }