4 * Copyright 2014-2015 Google Inc.
5 * Copyright 2014-2015 Linaro Ltd.
7 * Released under the GPLv2 only.
12 /* Endo ID (16 bits long) Masks */
13 #define ENDO_ID_MASK 0xFFFF
14 #define ENDO_LARGE_MASK 0x1000
15 #define ENDO_MEDIUM_MASK 0x0400
16 #define ENDO_MINI_MASK 0x0100
18 #define ENDO_FRONT_MASK(id) ((id) >> 13)
19 #define ENDO_BACK_SIDE_RIBS_MASK(ribs) ((1 << (ribs)) - 1)
22 * endo_is_medium() should be used only if endo isn't large. And endo_is_mini()
23 * should be used only if endo isn't large or medium.
25 #define endo_is_large(id) ((id) & ENDO_LARGE_MASK)
26 #define endo_is_medium(id) ((id) & ENDO_MEDIUM_MASK)
27 #define endo_is_mini(id) ((id) & ENDO_MINI_MASK)
29 #define endo_back_left_ribs(id, ribs) (((id) >> (ribs)) & ENDO_BACK_SIDE_RIBS_MASK(ribs))
30 #define endo_back_right_ribs(id, ribs) ((id) & ENDO_BACK_SIDE_RIBS_MASK(ribs))
33 * An Endo has interface block positions on the front and back.
34 * Each has numeric ID, starting with 1 (interface 0 represents
35 * the SVC within the Endo itself). The maximum interface ID is the
36 * also the number of non-SVC interfaces possible on the endo.
38 * Total number of interfaces:
40 * - Back left: max_ribs + 1
41 * - Back right: max_ribs + 1
43 #define max_endo_interface_id(endo_layout) \
44 (4 + ((endo_layout)->max_ribs + 1) * 2)
46 static struct ida greybus_endo_id_map;
48 /* endo sysfs attributes */
49 static ssize_t serial_number_show(struct device *dev,
50 struct device_attribute *attr, char *buf)
52 struct gb_endo *endo = to_gb_endo(dev);
54 return sprintf(buf, "%s\n", &endo->svc_info.serial_number[0]);
56 static DEVICE_ATTR_RO(serial_number);
58 static ssize_t version_show(struct device *dev,
59 struct device_attribute *attr, char *buf)
61 struct gb_endo *endo = to_gb_endo(dev);
63 return sprintf(buf, "%s\n", &endo->svc_info.version[0]);
65 static DEVICE_ATTR_RO(version);
67 static struct attribute *svc_attrs[] = {
68 &dev_attr_serial_number.attr,
69 &dev_attr_version.attr,
73 static const struct attribute_group svc_group = {
78 static ssize_t id_show(struct device *dev,
79 struct device_attribute *attr, char *buf)
81 struct gb_endo *endo = to_gb_endo(dev);
83 return sprintf(buf, "0x%04x\n", endo->id);
85 static DEVICE_ATTR_RO(id);
87 static ssize_t ap_intf_id_show(struct device *dev,
88 struct device_attribute *attr, char *buf)
90 struct gb_endo *endo = to_gb_endo(dev);
92 return sprintf(buf, "%u\n", endo->ap_intf_id);
94 static DEVICE_ATTR_RO(ap_intf_id);
96 static struct attribute *endo_attrs[] = {
98 &dev_attr_ap_intf_id.attr,
102 static const struct attribute_group endo_group = {
106 static const struct attribute_group *endo_groups[] = {
112 static void gb_endo_release(struct device *dev)
114 struct gb_endo *endo = to_gb_endo(dev);
116 ida_simple_remove(&greybus_endo_id_map, endo->dev_id);
120 struct device_type greybus_endo_type = {
121 .name = "greybus_endo",
122 .release = gb_endo_release,
126 /* Validate Endo ID */
129 * The maximum module height is 2 units. This means any adjacent pair of bits
130 * in the left or right mask must have at least one bit set.
132 static inline bool modules_oversized(unsigned int count, unsigned int mask)
136 for (i = 0; i < count - 1; i++)
137 if (!(mask & (0x3 << i)))
143 /* Reverse a number of least significant bits in a value */
144 static u8 reverse_bits(unsigned int value, unsigned int bits)
147 u8 result_mask = 1 << (bits - 1);
150 while (value && result_mask) {
151 if (value & value_mask) {
152 result |= result_mask;
163 * An Endo can have at most one instance of a single rib spanning its whole
164 * width. That is, the left and right bit masks representing the rib positions
165 * must have at most one bit set in both masks.
167 static bool single_cross_rib(u8 left_ribs, u8 right_ribs)
169 u8 span_ribs = left_ribs & right_ribs;
172 if (span_ribs & (span_ribs - 1))
178 * Each Endo size has its own set of front module configurations. For most, the
179 * resulting rib mask is the same regardless of the Endo size. The mini Endo
180 * has a few differences though.
182 * Endo front has 4 interface blocks and 3 rib positions. A maximum of 2 ribs
183 * are allowed to be present for any endo type.
185 * This routine validates front mask and sets 'front_ribs', its 3 least
186 * significant bits represent front ribs mask, other are 0. The front values
187 * should be within range (1..6).
189 * front_ribs bitmask:
190 * - Bit 0: 1st rib location from top, i.e. between interface 1 and 2.
191 * - Bit 1: 2nd rib location from top, i.e. between interface 2 and 3.
192 * - Bit 2: 3rd rib location from top, i.e. between interface 3 and 4.
194 static bool validate_front_ribs(struct gb_host_device *hd,
195 struct endo_layout *layout, bool mini,
198 u8 front_mask = ENDO_FRONT_MASK(endo_id);
200 /* Verify front endo mask is in valid range, i.e. 1-6 */
202 switch (front_mask) {
204 layout->front_ribs = 0x0;
207 layout->front_ribs = 0x1;
210 layout->front_ribs = 0x4;
213 layout->front_ribs = mini ? 0x2 : 0x3;
216 layout->front_ribs = mini ? 0x2 : 0x6;
219 layout->front_ribs = 0x5;
223 "%s: Invalid endo front mask 0x%02x, id 0x%04x\n",
224 __func__, front_mask, endo_id);
232 * The rear of an endo has a single vertical "spine", and the modules placed on
233 * the left and right of that spine are separated by ribs. Only one "cross"
234 * (i.e. rib that spans the entire width) is allowed of the back of the endo;
235 * all other ribs reach from the spine to the left or right edge.
237 * The width of the module positions on the left and right of the spine are
238 * determined by the width of the endo (either 1 or 2 "units"). The height of
239 * the modules is determined by the placement of the ribs (a module can be
240 * either 1 or 2 units high).
242 * The lower 13 bits of the 16-bit endo id are used to encode back ribs
243 * information. The large form factor endo uses all of these bits; the medium
244 * and mini form factors leave some bits unused (such bits shall be ignored, and
245 * are 0 for the purposes of this endo id definition).
247 * Each defined bit represents a rib position on one or the other side
248 * of the spine on the back of an endo. If that bit is set (1), it
249 * means a rib is present in the corresponding location; otherwise
250 * there is no rib there.
252 * Rotating an endo 180 degrees does not produce a new rib configuration. A
253 * single endo id represents a specific configuration of ribs without regard to
254 * its rotational orientation. We define one canonical id to represent a
255 * particular endo configuration.
257 static bool validate_back_ribs(struct gb_host_device *hd,
258 struct endo_layout *layout, u16 endo_id)
260 u8 max_ribs = layout->max_ribs;
264 /* Extract the left and right rib masks */
265 left_ribs = endo_back_left_ribs(endo_id, max_ribs);
266 right_ribs = endo_back_right_ribs(endo_id, max_ribs);
268 if (!single_cross_rib(left_ribs, right_ribs)) {
270 "%s: More than one spanning rib (left 0x%02x right 0x%02x), id 0x%04x\n",
271 __func__, left_ribs, right_ribs, endo_id);
275 if (modules_oversized(max_ribs, left_ribs)) {
277 "%s: Oversized module (left) 0x%02x, id 0x%04x\n",
278 __func__, left_ribs, endo_id);
282 if (modules_oversized(max_ribs, right_ribs)) {
284 "%s: Oversized module (Right) 0x%02x, id 0x%04x\n",
285 __func__, right_ribs, endo_id);
290 * The Endo numbering scheme represents the left and right rib
291 * configuration in a way that's convenient for looking for multiple
292 * spanning ribs. But it doesn't match the normal Endo interface
293 * numbering scheme (increasing counter-clockwise around the back).
294 * Reverse the right bit positions so they do match.
296 right_ribs = reverse_bits(right_ribs, max_ribs);
299 * A mini or large Endo rotated 180 degrees is still the same Endo. In
300 * most cases that allows two distinct values to represent the same
301 * Endo; we choose one of them to be the canonical one (and the other is
302 * invalid). The canonical one is identified by higher value of left
305 * This doesn't apply to medium Endos, because the left and right sides
306 * are of different widths.
308 if (max_ribs != ENDO_BACK_RIBS_MEDIUM && left_ribs < right_ribs) {
309 dev_err(&hd->dev, "%s: Non-canonical endo id 0x%04x\n", __func__,
314 layout->left_ribs = left_ribs;
315 layout->right_ribs = right_ribs;
320 * Validate the endo-id passed from SVC. Error out if its not a valid Endo,
321 * else return structure representing ribs positions on front and back of Endo.
323 static int gb_endo_validate_id(struct gb_host_device *hd,
324 struct endo_layout *layout, u16 endo_id)
326 /* Validate Endo Size */
327 if (endo_is_large(endo_id)) {
328 /* Large Endo type */
329 layout->max_ribs = ENDO_BACK_RIBS_LARGE;
330 } else if (endo_is_medium(endo_id)) {
331 /* Medium Endo type */
332 layout->max_ribs = ENDO_BACK_RIBS_MEDIUM;
333 } else if (endo_is_mini(endo_id)) {
335 layout->max_ribs = ENDO_BACK_RIBS_MINI;
337 dev_err(&hd->dev, "%s: Invalid endo type, id 0x%04x\n",
342 if (!validate_back_ribs(hd, layout, endo_id))
345 if (!validate_front_ribs(hd, layout,
346 layout->max_ribs == ENDO_BACK_RIBS_MINI,
354 * Look up which module contains the given interface.
356 * A module's ID is the same as its lowest-numbered interface ID. So the module
357 * ID for a 1x1 module is always the same as its interface ID.
360 * The module ID for an interface on a 1x2 or 2x2 module (which use two
361 * interface blocks) can be either the interface ID, or one less than the
362 * interface ID if there is no rib "above" the interface.
365 * There are three rib locations in front and all of them might be unused, i.e.
366 * a single module is used for all 4 interfaces. We need to check all ribs in
367 * that case to find module ID.
369 u8 endo_get_module_id(struct gb_endo *endo, u8 interface_id)
371 struct endo_layout *layout = &endo->layout;
372 unsigned int height = layout->max_ribs + 1;
373 unsigned int iid = interface_id - 1;
374 unsigned int mask, rib_mask;
379 if (iid < height) { /* back left */
380 mask = layout->left_ribs;
381 } else if (iid < 2 * height) { /* back right */
382 mask = layout->right_ribs;
385 mask = layout->front_ribs;
390 * Find the next rib *above* this interface to determine the lowest
391 * interface ID in the module.
394 while ((rib_mask >>= 1) != 0 && !(mask & rib_mask))
401 * Creates all possible modules for the Endo.
403 * We try to create modules for all possible interface IDs. If a module is
404 * already created, we skip creating it again with the help of prev_module_id.
406 static int create_modules(struct gb_endo *endo)
408 struct gb_module *module;
409 int prev_module_id = 0;
414 max_id = max_endo_interface_id(&endo->layout);
416 /* Find module corresponding to each interface */
417 for (interface_id = 1; interface_id <= max_id; interface_id++) {
418 module_id = endo_get_module_id(endo, interface_id);
420 if (WARN_ON(!module_id))
423 /* Skip already created modules */
424 if (module_id == prev_module_id)
427 prev_module_id = module_id;
429 /* New module, create it */
430 module = gb_module_create(&endo->dev, module_id);
438 static int gb_endo_register(struct gb_host_device *hd,
439 struct gb_endo *endo)
444 dev_id = ida_simple_get(&greybus_endo_id_map, 0, 0, GFP_KERNEL);
448 endo->dev_id = dev_id;
450 endo->dev.parent = &hd->dev;
451 endo->dev.bus = &greybus_bus_type;
452 endo->dev.type = &greybus_endo_type;
453 endo->dev.groups = endo_groups;
454 endo->dev.dma_mask = hd->dev.dma_mask;
455 device_initialize(&endo->dev);
456 dev_set_name(&endo->dev, "endo%hu", endo->dev_id);
459 // Get the version and serial number from the SVC, right now we are
460 // using "fake" numbers.
461 strcpy(&endo->svc_info.serial_number[0], "042");
462 strcpy(&endo->svc_info.version[0], "0.0");
464 retval = device_add(&endo->dev);
466 dev_err(&hd->dev, "failed to add endo device of id 0x%04x\n",
468 put_device(&endo->dev);
474 struct gb_endo *gb_endo_create(struct gb_host_device *hd, u16 endo_id,
477 struct gb_endo *endo;
480 endo = kzalloc(sizeof(*endo), GFP_KERNEL);
482 return ERR_PTR(-ENOMEM);
484 /* First check if the value supplied is a valid endo id */
485 if (gb_endo_validate_id(hd, &endo->layout, endo_id)) {
489 if (ap_intf_id > max_endo_interface_id(&endo->layout)) {
494 endo->ap_intf_id = ap_intf_id;
496 /* Register Endo device */
497 retval = gb_endo_register(hd, endo);
501 /* Create modules/interfaces */
502 retval = create_modules(endo);
504 gb_endo_remove(endo);
513 return ERR_PTR(retval);
516 void gb_endo_remove(struct gb_endo *endo)
521 /* remove all modules for this endo */
522 gb_module_remove_all(endo);
524 device_unregister(&endo->dev);
527 int greybus_endo_setup(struct gb_host_device *hd, u16 endo_id,
530 struct gb_endo *endo;
532 endo = gb_endo_create(hd, endo_id, ap_intf_id);
534 return PTR_ERR(endo);
539 EXPORT_SYMBOL_GPL(greybus_endo_setup);
541 int __init gb_endo_init(void)
543 ida_init(&greybus_endo_id_map);
548 void gb_endo_exit(void)
550 ida_destroy(&greybus_endo_id_map);