xref: /linux/drivers/ssb/sprom.c (revision 4b4193256c8d3bc3a5397b5cd9494c2ad386317d)
1e7ec2e32SMichael Buesch /*
2e7ec2e32SMichael Buesch  * Sonics Silicon Backplane
3e7ec2e32SMichael Buesch  * Common SPROM support routines
4e7ec2e32SMichael Buesch  *
5eb032b98SMichael Büsch  * Copyright (C) 2005-2008 Michael Buesch <m@bues.ch>
6e7ec2e32SMichael Buesch  * Copyright (C) 2005 Martin Langer <martin-langer@gmx.de>
7e7ec2e32SMichael Buesch  * Copyright (C) 2005 Stefano Brivio <st3@riseup.net>
8e7ec2e32SMichael Buesch  * Copyright (C) 2005 Danny van Dyk <kugelfang@gentoo.org>
9e7ec2e32SMichael Buesch  * Copyright (C) 2005 Andreas Jaggi <andreas.jaggi@waterwave.ch>
10e7ec2e32SMichael Buesch  *
11e7ec2e32SMichael Buesch  * Licensed under the GNU/GPL. See COPYING for details.
12e7ec2e32SMichael Buesch  */
13e7ec2e32SMichael Buesch 
14e7ec2e32SMichael Buesch #include "ssb_private.h"
15e7ec2e32SMichael Buesch 
16e33761e6SMichael Buesch #include <linux/ctype.h>
175a0e3ad6STejun Heo #include <linux/slab.h>
18e33761e6SMichael Buesch 
19e7ec2e32SMichael Buesch 
20b3ae52b6SHauke Mehrtens static int(*get_fallback_sprom)(struct ssb_bus *dev, struct ssb_sprom *out);
21e79c1ba8SMichael Buesch 
22e79c1ba8SMichael Buesch 
sprom2hex(const u16 * sprom,char * buf,size_t buf_len,size_t sprom_size_words)23e7ec2e32SMichael Buesch static int sprom2hex(const u16 *sprom, char *buf, size_t buf_len,
24e7ec2e32SMichael Buesch 		     size_t sprom_size_words)
25e7ec2e32SMichael Buesch {
26e7ec2e32SMichael Buesch 	int i, pos = 0;
27e7ec2e32SMichael Buesch 
28e7ec2e32SMichael Buesch 	for (i = 0; i < sprom_size_words; i++)
29ca44e47aSTakashi Iwai 		pos += scnprintf(buf + pos, buf_len - pos - 1,
30e7ec2e32SMichael Buesch 				"%04X", swab16(sprom[i]) & 0xFFFF);
31ca44e47aSTakashi Iwai 	pos += scnprintf(buf + pos, buf_len - pos - 1, "\n");
32e7ec2e32SMichael Buesch 
33e7ec2e32SMichael Buesch 	return pos + 1;
34e7ec2e32SMichael Buesch }
35e7ec2e32SMichael Buesch 
hex2sprom(u16 * sprom,const char * dump,size_t len,size_t sprom_size_words)36e7ec2e32SMichael Buesch static int hex2sprom(u16 *sprom, const char *dump, size_t len,
37e7ec2e32SMichael Buesch 		     size_t sprom_size_words)
38e7ec2e32SMichael Buesch {
39e33761e6SMichael Buesch 	char c, tmp[5] = { 0 };
40e33761e6SMichael Buesch 	int err, cnt = 0;
41e7ec2e32SMichael Buesch 	unsigned long parsed;
42e7ec2e32SMichael Buesch 
43e33761e6SMichael Buesch 	/* Strip whitespace at the end. */
44e33761e6SMichael Buesch 	while (len) {
45e33761e6SMichael Buesch 		c = dump[len - 1];
46e33761e6SMichael Buesch 		if (!isspace(c) && c != '\0')
47e33761e6SMichael Buesch 			break;
48e33761e6SMichael Buesch 		len--;
49e33761e6SMichael Buesch 	}
50e33761e6SMichael Buesch 	/* Length must match exactly. */
51e33761e6SMichael Buesch 	if (len != sprom_size_words * 4)
52e7ec2e32SMichael Buesch 		return -EINVAL;
53e7ec2e32SMichael Buesch 
54e7ec2e32SMichael Buesch 	while (cnt < sprom_size_words) {
55e7ec2e32SMichael Buesch 		memcpy(tmp, dump, 4);
56e7ec2e32SMichael Buesch 		dump += 4;
576b1ea4b2SJingoo Han 		err = kstrtoul(tmp, 16, &parsed);
58e33761e6SMichael Buesch 		if (err)
59e33761e6SMichael Buesch 			return err;
60e7ec2e32SMichael Buesch 		sprom[cnt++] = swab16((u16)parsed);
61e7ec2e32SMichael Buesch 	}
62e7ec2e32SMichael Buesch 
63e7ec2e32SMichael Buesch 	return 0;
64e7ec2e32SMichael Buesch }
65e7ec2e32SMichael Buesch 
66e7ec2e32SMichael Buesch /* Common sprom device-attribute show-handler */
ssb_attr_sprom_show(struct ssb_bus * bus,char * buf,int (* sprom_read)(struct ssb_bus * bus,u16 * sprom))67e7ec2e32SMichael Buesch ssize_t ssb_attr_sprom_show(struct ssb_bus *bus, char *buf,
68e7ec2e32SMichael Buesch 			    int (*sprom_read)(struct ssb_bus *bus, u16 *sprom))
69e7ec2e32SMichael Buesch {
70e7ec2e32SMichael Buesch 	u16 *sprom;
71e7ec2e32SMichael Buesch 	int err = -ENOMEM;
72e7ec2e32SMichael Buesch 	ssize_t count = 0;
73e7ec2e32SMichael Buesch 	size_t sprom_size_words = bus->sprom_size;
74e7ec2e32SMichael Buesch 
75e7ec2e32SMichael Buesch 	sprom = kcalloc(sprom_size_words, sizeof(u16), GFP_KERNEL);
76e7ec2e32SMichael Buesch 	if (!sprom)
77e7ec2e32SMichael Buesch 		goto out;
78e7ec2e32SMichael Buesch 
79e7ec2e32SMichael Buesch 	/* Use interruptible locking, as the SPROM write might
80e7ec2e32SMichael Buesch 	 * be holding the lock for several seconds. So allow userspace
812aad9f81SJohn Oldman 	 * to cancel operation.
822aad9f81SJohn Oldman 	 */
83e7ec2e32SMichael Buesch 	err = -ERESTARTSYS;
84e7ec2e32SMichael Buesch 	if (mutex_lock_interruptible(&bus->sprom_mutex))
85e7ec2e32SMichael Buesch 		goto out_kfree;
86e7ec2e32SMichael Buesch 	err = sprom_read(bus, sprom);
87e7ec2e32SMichael Buesch 	mutex_unlock(&bus->sprom_mutex);
88e7ec2e32SMichael Buesch 
89e7ec2e32SMichael Buesch 	if (!err)
90e7ec2e32SMichael Buesch 		count = sprom2hex(sprom, buf, PAGE_SIZE, sprom_size_words);
91e7ec2e32SMichael Buesch 
92e7ec2e32SMichael Buesch out_kfree:
93e7ec2e32SMichael Buesch 	kfree(sprom);
94e7ec2e32SMichael Buesch out:
95e7ec2e32SMichael Buesch 	return err ? err : count;
96e7ec2e32SMichael Buesch }
97e7ec2e32SMichael Buesch 
98e7ec2e32SMichael Buesch /* Common sprom device-attribute store-handler */
ssb_attr_sprom_store(struct ssb_bus * bus,const char * buf,size_t count,int (* sprom_check_crc)(const u16 * sprom,size_t size),int (* sprom_write)(struct ssb_bus * bus,const u16 * sprom))99e7ec2e32SMichael Buesch ssize_t ssb_attr_sprom_store(struct ssb_bus *bus,
100e7ec2e32SMichael Buesch 			     const char *buf, size_t count,
101e7ec2e32SMichael Buesch 			     int (*sprom_check_crc)(const u16 *sprom, size_t size),
102e7ec2e32SMichael Buesch 			     int (*sprom_write)(struct ssb_bus *bus, const u16 *sprom))
103e7ec2e32SMichael Buesch {
104e7ec2e32SMichael Buesch 	u16 *sprom;
105e7ec2e32SMichael Buesch 	int res = 0, err = -ENOMEM;
106e7ec2e32SMichael Buesch 	size_t sprom_size_words = bus->sprom_size;
1073ba6018aSMichael Buesch 	struct ssb_freeze_context freeze;
108e7ec2e32SMichael Buesch 
109e7ec2e32SMichael Buesch 	sprom = kcalloc(bus->sprom_size, sizeof(u16), GFP_KERNEL);
110e7ec2e32SMichael Buesch 	if (!sprom)
111e7ec2e32SMichael Buesch 		goto out;
112e7ec2e32SMichael Buesch 	err = hex2sprom(sprom, buf, count, sprom_size_words);
113e7ec2e32SMichael Buesch 	if (err) {
114e7ec2e32SMichael Buesch 		err = -EINVAL;
115e7ec2e32SMichael Buesch 		goto out_kfree;
116e7ec2e32SMichael Buesch 	}
117e7ec2e32SMichael Buesch 	err = sprom_check_crc(sprom, sprom_size_words);
118e7ec2e32SMichael Buesch 	if (err) {
119e7ec2e32SMichael Buesch 		err = -EINVAL;
120e7ec2e32SMichael Buesch 		goto out_kfree;
121e7ec2e32SMichael Buesch 	}
122e7ec2e32SMichael Buesch 
123e7ec2e32SMichael Buesch 	/* Use interruptible locking, as the SPROM write might
124e7ec2e32SMichael Buesch 	 * be holding the lock for several seconds. So allow userspace
1252aad9f81SJohn Oldman 	 * to cancel operation.
1262aad9f81SJohn Oldman 	 */
127e7ec2e32SMichael Buesch 	err = -ERESTARTSYS;
128e7ec2e32SMichael Buesch 	if (mutex_lock_interruptible(&bus->sprom_mutex))
129e7ec2e32SMichael Buesch 		goto out_kfree;
1303ba6018aSMichael Buesch 	err = ssb_devices_freeze(bus, &freeze);
131e7ec2e32SMichael Buesch 	if (err) {
132b8b6069cSMichael Büsch 		pr_err("SPROM write: Could not freeze all devices\n");
133e7ec2e32SMichael Buesch 		goto out_unlock;
134e7ec2e32SMichael Buesch 	}
135e7ec2e32SMichael Buesch 	res = sprom_write(bus, sprom);
1363ba6018aSMichael Buesch 	err = ssb_devices_thaw(&freeze);
137e7ec2e32SMichael Buesch 	if (err)
138b8b6069cSMichael Büsch 		pr_err("SPROM write: Could not thaw all devices\n");
139e7ec2e32SMichael Buesch out_unlock:
140e7ec2e32SMichael Buesch 	mutex_unlock(&bus->sprom_mutex);
141e7ec2e32SMichael Buesch out_kfree:
142e7ec2e32SMichael Buesch 	kfree(sprom);
143e7ec2e32SMichael Buesch out:
144e7ec2e32SMichael Buesch 	if (res)
145e7ec2e32SMichael Buesch 		return res;
146e7ec2e32SMichael Buesch 	return err ? err : count;
147e7ec2e32SMichael Buesch }
148e79c1ba8SMichael Buesch 
149e79c1ba8SMichael Buesch /**
150b3ae52b6SHauke Mehrtens  * ssb_arch_register_fallback_sprom - Registers a method providing a
151b3ae52b6SHauke Mehrtens  * fallback SPROM if no SPROM is found.
152e79c1ba8SMichael Buesch  *
153b3ae52b6SHauke Mehrtens  * @sprom_callback: The callback function.
154e79c1ba8SMichael Buesch  *
155b3ae52b6SHauke Mehrtens  * With this function the architecture implementation may register a
156b3ae52b6SHauke Mehrtens  * callback handler which fills the SPROM data structure. The fallback is
157b3ae52b6SHauke Mehrtens  * only used for PCI based SSB devices, where no valid SPROM can be found
158b3ae52b6SHauke Mehrtens  * in the shadow registers.
159e79c1ba8SMichael Buesch  *
160b3ae52b6SHauke Mehrtens  * This function is useful for weird architectures that have a half-assed
161b3ae52b6SHauke Mehrtens  * SSB device hardwired to their PCI bus.
162e79c1ba8SMichael Buesch  *
163b3ae52b6SHauke Mehrtens  * Note that it does only work with PCI attached SSB devices. PCMCIA
164b3ae52b6SHauke Mehrtens  * devices currently don't use this fallback.
165b3ae52b6SHauke Mehrtens  * Architectures must provide the SPROM for native SSB devices anyway, so
166b3ae52b6SHauke Mehrtens  * the fallback also isn't used for native devices.
167e79c1ba8SMichael Buesch  *
168b3ae52b6SHauke Mehrtens  * This function is available for architecture code, only. So it is not
169b3ae52b6SHauke Mehrtens  * exported.
170e79c1ba8SMichael Buesch  */
ssb_arch_register_fallback_sprom(int (* sprom_callback)(struct ssb_bus * bus,struct ssb_sprom * out))171b3ae52b6SHauke Mehrtens int ssb_arch_register_fallback_sprom(int (*sprom_callback)(struct ssb_bus *bus,
172b3ae52b6SHauke Mehrtens 				     struct ssb_sprom *out))
173e79c1ba8SMichael Buesch {
174b3ae52b6SHauke Mehrtens 	if (get_fallback_sprom)
175e79c1ba8SMichael Buesch 		return -EEXIST;
176b3ae52b6SHauke Mehrtens 	get_fallback_sprom = sprom_callback;
177e79c1ba8SMichael Buesch 
178e79c1ba8SMichael Buesch 	return 0;
179e79c1ba8SMichael Buesch }
180e79c1ba8SMichael Buesch 
ssb_fill_sprom_with_fallback(struct ssb_bus * bus,struct ssb_sprom * out)181b3ae52b6SHauke Mehrtens int ssb_fill_sprom_with_fallback(struct ssb_bus *bus, struct ssb_sprom *out)
182e79c1ba8SMichael Buesch {
183b3ae52b6SHauke Mehrtens 	if (!get_fallback_sprom)
184b3ae52b6SHauke Mehrtens 		return -ENOENT;
185b3ae52b6SHauke Mehrtens 
186b3ae52b6SHauke Mehrtens 	return get_fallback_sprom(bus, out);
187e79c1ba8SMichael Buesch }
188d53cdbb9SJohn W. Linville 
189*0db5bc7bSAlexander A. Klimov /* https://bcm-v4.sipsolutions.net/802.11/IsSpromAvailable */
ssb_is_sprom_available(struct ssb_bus * bus)190d53cdbb9SJohn W. Linville bool ssb_is_sprom_available(struct ssb_bus *bus)
191d53cdbb9SJohn W. Linville {
192d53cdbb9SJohn W. Linville 	/* status register only exists on chipcomon rev >= 11 and we need check
1932aad9f81SJohn Oldman 	 * for >= 31 only
1942aad9f81SJohn Oldman 	 */
195d53cdbb9SJohn W. Linville 	/* this routine differs from specs as we do not access SPROM directly
1962aad9f81SJohn Oldman 	 * on PCMCIA
1972aad9f81SJohn Oldman 	 */
198d53cdbb9SJohn W. Linville 	if (bus->bustype == SSB_BUSTYPE_PCI &&
19925985edcSLucas De Marchi 	    bus->chipco.dev &&	/* can be unavailable! */
200d53cdbb9SJohn W. Linville 	    bus->chipco.dev->id.revision >= 31)
201d53cdbb9SJohn W. Linville 		return bus->chipco.capabilities & SSB_CHIPCO_CAP_SPROM;
202d53cdbb9SJohn W. Linville 
203d53cdbb9SJohn W. Linville 	return true;
204d53cdbb9SJohn W. Linville }
205