2 * Copyright (c) 2012 GCT Semiconductor, Inc. All rights reserved.
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.
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.
14 #include <linux/module.h>
15 #include <linux/version.h>
16 #include <linux/kernel.h>
17 #include <linux/init.h>
19 #include <linux/uaccess.h>
21 #include <linux/sched.h>
22 #include <linux/slab.h>
24 #include <linux/mmc/core.h>
25 #include <linux/mmc/card.h>
26 #include <linux/mmc/sdio_func.h>
30 #define TYPE_A_HEADER_SIZE 4
31 #define TYPE_A_LOOKAHEAD_SIZE 16
32 #define YMEM0_SIZE 0x8000 /* 32kbytes */
33 #define DOWNLOAD_SIZE (YMEM0_SIZE - TYPE_A_HEADER_SIZE)
35 #define KRN_PATH "/lib/firmware/gdm72xx/gdmskrn.bin"
36 #define RFS_PATH "/lib/firmware/gdm72xx/gdmsrfs.bin"
40 static int ack_ready(struct sdio_func *func)
42 unsigned long start = jiffies;
46 while ((jiffies - start) < HZ) {
47 val = sdio_readb(func, 0x13, &ret);
56 static int download_image(struct sdio_func *func, char *img_name)
58 int ret = 0, len, size, pno;
59 struct file *filp = NULL;
60 struct inode *inode = NULL;
64 filp = filp_open(img_name, O_RDONLY | O_LARGEFILE, 0);
66 printk(KERN_ERR "Can't find %s.\n", img_name);
71 inode = filp->f_dentry->d_inode;
72 if (!inode || !S_ISREG(inode->i_mode)) {
73 printk(KERN_ERR "Invalid file type: %s\n", img_name);
78 size = i_size_read(inode->i_mapping->host);
80 printk(KERN_ERR "Unable to find file size: %s\n", img_name);
86 while ((len = filp->f_op->read(filp, buf + TYPE_A_HEADER_SIZE,
87 DOWNLOAD_SIZE, &pos))) {
94 buf[1] = (len >> 8) & 0xff;
95 buf[2] = (len >> 16) & 0xff;
97 if (pos >= size) /* The last packet */
102 ret = sdio_memcpy_toio(func, 0, buf, len + TYPE_A_HEADER_SIZE);
104 printk(KERN_ERR "gdmwm: send image error: "
105 "packet number = %d ret = %d\n", pno, ret);
108 if (buf[3] == 2) /* The last packet */
110 if (!ack_ready(func)) {
112 printk(KERN_ERR "gdmwm: Ack is not ready.\n");
115 ret = sdio_memcpy_fromio(func, buf, 0, TYPE_A_LOOKAHEAD_SIZE);
117 printk(KERN_ERR "gdmwm: receive ack error: "
118 "packet number = %d ret = %d\n", pno, ret);
121 sdio_writeb(func, 0x01, 0x13, &ret);
122 sdio_writeb(func, 0x00, 0x10, &ret); /* PCRRT */
127 filp_close(filp, current->files);
131 int sdio_boot(struct sdio_func *func)
133 static mm_segment_t fs;
136 tx_buf = kmalloc(YMEM0_SIZE, GFP_KERNEL);
137 if (tx_buf == NULL) {
138 printk(KERN_ERR "Error: kmalloc: %s %d\n", __func__, __LINE__);
145 ret = download_image(func, KRN_PATH);
148 printk(KERN_INFO "GCT: Kernel download success.\n");
150 ret = download_image(func, RFS_PATH);
153 printk(KERN_INFO "GCT: Filesystem download success.\n");