aboutsummaryrefslogtreecommitdiffstats
path: root/lib/mpi/mpiutil.c
blob: 657979f71bef0a0b3331eb804c0921d1f7afe6a8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/* mpiutil.ac  -  Utility functions for MPI
 * Copyright (C) 1998, 1999 Free Software Foundation, Inc.
 *
 * This file is part of GnuPG.
 *
 * GnuPG is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * GnuPG is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
 */

#include "mpi-internal.h"

/****************
 * Note:  It was a bad idea to use the number of limbs to allocate
 *	  because on a alpha the limbs are large but we normally need
 *	  integers of n bits - So we should chnage this to bits (or bytes).
 *
 *	  But mpi_alloc is used in a lot of places :-)
 */
MPI mpi_alloc(unsigned nlimbs)
{
	MPI a;

	a = kmalloc(sizeof *a, GFP_KERNEL);
	if (!a)
		return a;

	if (nlimbs) {
		a->d = mpi_alloc_limb_space(nlimbs);
		if (!a->d) {
			kfree(a);
			return NULL;
		}
	} else {
		a->d = NULL;
	}

	a->alloced = nlimbs;
	a->nlimbs = 0;
	a->sign = 0;
	a->flags = 0;
	a->nbits = 0;
	return a;
}
EXPORT_SYMBOL_GPL(mpi_alloc);

mpi_ptr_t mpi_alloc_limb_space(unsigned nlimbs)
{
	size_t len = nlimbs * sizeof(mpi_limb_t);

	if (!len)
		return NULL;

	return kmalloc(len, GFP_KERNEL);
}

void mpi_free_limb_space(mpi_ptr_t a)
{
	if (!a)
		return;

	kfree(a);
}

void mpi_assign_limb_space(MPI a, mpi_ptr_t ap, unsigned nlimbs)
{
	mpi_free_limb_space(a->d);
	a->d = ap;
	a->alloced = nlimbs;
}

/****************
 * Resize the array of A to NLIMBS. the additional space is cleared
 * (set to 0) [done by m_realloc()]
 */
int mpi_resize(MPI a, unsigned nlimbs)
{
	void *p;

	if (nlimbs <= a->alloced)
		return 0;	/* no need to do it */

	if (a->d) {
		p = kmalloc(nlimbs * sizeof(mpi_limb_t), GFP_KERNEL);
		if (!p)
			return -ENOMEM;
		memcpy(p, a->d, a->alloced * sizeof(mpi_limb_t));
		kfree(a->d);
		a->d = p;
	} else {
		a->d = kzalloc(nlimbs * sizeof(mpi_limb_t), GFP_KERNEL);
		if (!a->d)
			return -ENOMEM;
	}
	a->alloced = nlimbs;
	return 0;
}

void mpi_free(MPI a)
{
	if (!a)
		return;

	if (a->flags & 4)
		kfree(a->d);
	else
		mpi_free_limb_space(a->d);

	if (a->flags & ~7)
		pr_info("invalid flag value in mpi\n");
	kfree(a);
}
EXPORT_SYMBOL_GPL(mpi_free);
hl opt">; spi_message_init(&m); memset(&t, 0, sizeof(struct spi_transfer)); cmd_resp[0] = SST25L_CMD_RDSR; cmd_resp[1] = 0xff; t.tx_buf = cmd_resp; t.rx_buf = cmd_resp; t.len = sizeof(cmd_resp); spi_message_add_tail(&t, &m); err = spi_sync(flash->spi, &m); if (err < 0) return err; *status = cmd_resp[1]; return 0; } static int sst25l_write_enable(struct sst25l_flash *flash, int enable) { unsigned char command[2]; int status, err; command[0] = enable ? SST25L_CMD_WREN : SST25L_CMD_WRDI; err = spi_write(flash->spi, command, 1); if (err) return err; command[0] = SST25L_CMD_EWSR; err = spi_write(flash->spi, command, 1); if (err) return err; command[0] = SST25L_CMD_WRSR; command[1] = enable ? 0 : SST25L_STATUS_BP0 | SST25L_STATUS_BP1; err = spi_write(flash->spi, command, 2); if (err) return err; if (enable) { err = sst25l_status(flash, &status); if (err) return err; if (!(status & SST25L_STATUS_WREN)) return -EROFS; } return 0; } static int sst25l_wait_till_ready(struct sst25l_flash *flash) { unsigned long deadline; int status, err; deadline = jiffies + MAX_READY_WAIT_JIFFIES; do { err = sst25l_status(flash, &status); if (err) return err; if (!(status & SST25L_STATUS_BUSY)) return 0; cond_resched(); } while (!time_after_eq(jiffies, deadline)); return -ETIMEDOUT; } static int sst25l_erase_sector(struct sst25l_flash *flash, uint32_t offset) { unsigned char command[4]; int err; err = sst25l_write_enable(flash, 1); if (err) return err; command[0] = SST25L_CMD_SECTOR_ERASE; command[1] = offset >> 16; command[2] = offset >> 8; command[3] = offset; err = spi_write(flash->spi, command, 4); if (err) return err; err = sst25l_wait_till_ready(flash); if (err) return err; return sst25l_write_enable(flash, 0); } static int sst25l_erase(struct mtd_info *mtd, struct erase_info *instr) { struct sst25l_flash *flash = to_sst25l_flash(mtd); uint32_t addr, end; int err; /* Sanity checks */ if ((uint32_t)instr->len % mtd->erasesize) return -EINVAL; if ((uint32_t)instr->addr % mtd->erasesize) return -EINVAL; addr = instr->addr; end = addr + instr->len; mutex_lock(&flash->lock); err = sst25l_wait_till_ready(flash); if (err) { mutex_unlock(&flash->lock); return err; } while (addr < end) { err = sst25l_erase_sector(flash, addr); if (err) { mutex_unlock(&flash->lock); dev_err(&flash->spi->dev, "Erase failed\n"); return err; } addr += mtd->erasesize; } mutex_unlock(&flash->lock); return 0; } static int sst25l_read(struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen, unsigned char *buf) { struct sst25l_flash *flash = to_sst25l_flash(mtd); struct spi_transfer transfer[2]; struct spi_message message; unsigned char command[4]; int ret; spi_message_init(&message); memset(&transfer, 0, sizeof(transfer)); command[0] = SST25L_CMD_READ; command[1] = from >> 16; command[2] = from >> 8; command[3] = from; transfer[0].tx_buf = command; transfer[0].len = sizeof(command); spi_message_add_tail(&transfer[0], &message); transfer[1].rx_buf = buf; transfer[1].len = len; spi_message_add_tail(&transfer[1], &message); mutex_lock(&flash->lock); /* Wait for previous write/erase to complete */ ret = sst25l_wait_till_ready(flash); if (ret) { mutex_unlock(&flash->lock); return ret; } spi_sync(flash->spi, &message); if (retlen && message.actual_length > sizeof(command)) *retlen += message.actual_length - sizeof(command); mutex_unlock(&flash->lock); return 0; } static int sst25l_write(struct mtd_info *mtd, loff_t to, size_t len, size_t *retlen, const unsigned char *buf) { struct sst25l_flash *flash = to_sst25l_flash(mtd); int i, j, ret, bytes, copied = 0; unsigned char command[5]; if ((uint32_t)to % mtd->writesize) return -EINVAL; mutex_lock(&flash->lock); ret = sst25l_write_enable(flash, 1); if (ret) goto out; for (i = 0; i < len; i += mtd->writesize) { ret = sst25l_wait_till_ready(flash); if (ret) goto out; /* Write the first byte of the page */ command[0] = SST25L_CMD_AAI_PROGRAM; command[1] = (to + i) >> 16; command[2] = (to + i) >> 8; command[3] = (to + i); command[4] = buf[i]; ret = spi_write(flash->spi, command, 5); if (ret < 0) goto out; copied++; /* * Write the remaining bytes using auto address * increment mode */ bytes = min_t(uint32_t, mtd->writesize, len - i); for (j = 1; j < bytes; j++, copied++) { ret = sst25l_wait_till_ready(flash); if (ret) goto out; command[1] = buf[i + j]; ret = spi_write(flash->spi, command, 2); if (ret) goto out; } } out: ret = sst25l_write_enable(flash, 0); if (retlen) *retlen = copied; mutex_unlock(&flash->lock); return ret; } static struct flash_info *sst25l_match_device(struct spi_device *spi) { struct flash_info *flash_info = NULL; struct spi_message m; struct spi_transfer t; unsigned char cmd_resp[6]; int i, err; uint16_t id; spi_message_init(&m); memset(&t, 0, sizeof(struct spi_transfer)); cmd_resp[0] = SST25L_CMD_READ_ID; cmd_resp[1] = 0; cmd_resp[2] = 0; cmd_resp[3] = 0; cmd_resp[4] = 0xff; cmd_resp[5] = 0xff; t.tx_buf = cmd_resp; t.rx_buf = cmd_resp; t.len = sizeof(cmd_resp); spi_message_add_tail(&t, &m); err = spi_sync(spi, &m); if (err < 0) { dev_err(&spi->dev, "error reading device id\n"); return NULL; } id = (cmd_resp[4] << 8) | cmd_resp[5]; for (i = 0; i < ARRAY_SIZE(sst25l_flash_info); i++) if (sst25l_flash_info[i].device_id == id) flash_info = &sst25l_flash_info[i]; if (!flash_info) dev_err(&spi->dev, "unknown id %.4x\n", id); return flash_info; } static int sst25l_probe(struct spi_device *spi) { struct flash_info *flash_info; struct sst25l_flash *flash; struct flash_platform_data *data; int ret; flash_info = sst25l_match_device(spi); if (!flash_info) return -ENODEV; flash = devm_kzalloc(&spi->dev, sizeof(*flash), GFP_KERNEL); if (!flash) return -ENOMEM; flash->spi = spi; mutex_init(&flash->lock); spi_set_drvdata(spi, flash); data = dev_get_platdata(&spi->dev); if (data && data->name) flash->mtd.name = data->name; flash->mtd.dev.parent = &spi->dev; flash->mtd.type = MTD_NORFLASH; flash->mtd.flags = MTD_CAP_NORFLASH; flash->mtd.erasesize = flash_info->erase_size; flash->mtd.writesize = flash_info->page_size; flash->mtd.writebufsize = flash_info->page_size; flash->mtd.size = flash_info->page_size * flash_info->nr_pages; flash->mtd._erase = sst25l_erase; flash->mtd._read = sst25l_read; flash->mtd._write = sst25l_write; dev_info(&spi->dev, "%s (%lld KiB)\n", flash_info->name, (long long)flash->mtd.size >> 10); pr_debug("mtd .name = %s, .size = 0x%llx (%lldMiB) " ".erasesize = 0x%.8x (%uKiB) .numeraseregions = %d\n", flash->mtd.name, (long long)flash->mtd.size, (long long)(flash->mtd.size >> 20), flash->mtd.erasesize, flash->mtd.erasesize / 1024, flash->mtd.numeraseregions); ret = mtd_device_register(&flash->mtd, data ? data->parts : NULL, data ? data->nr_parts : 0); if (ret) return -ENODEV; return 0; } static int sst25l_remove(struct spi_device *spi) { struct sst25l_flash *flash = spi_get_drvdata(spi); return mtd_device_unregister(&flash->mtd); } static struct spi_driver sst25l_driver = { .driver = { .name = "sst25l", }, .probe = sst25l_probe, .remove = sst25l_remove, }; module_spi_driver(sst25l_driver); MODULE_DESCRIPTION("MTD SPI driver for SST25L Flash chips"); MODULE_AUTHOR("Andre Renaud <andre@bluewatersys.com>, " "Ryan Mallon"); MODULE_LICENSE("GPL");