1 // SPDX-License-Identifier: GPL-2.0-only
3 * dice_transaction.c - a part of driver for Dice based devices
5 * Copyright (c) Clemens Ladisch
6 * Copyright (c) 2014 Takashi Sakamoto
11 static u64 get_subaddr(struct snd_dice *dice, enum snd_dice_addr_type type,
15 case SND_DICE_ADDR_TYPE_TX:
16 offset += dice->tx_offset;
18 case SND_DICE_ADDR_TYPE_RX:
19 offset += dice->rx_offset;
21 case SND_DICE_ADDR_TYPE_SYNC:
22 offset += dice->sync_offset;
24 case SND_DICE_ADDR_TYPE_RSRV:
25 offset += dice->rsrv_offset;
27 case SND_DICE_ADDR_TYPE_GLOBAL:
29 offset += dice->global_offset;
32 offset += DICE_PRIVATE_SPACE;
36 int snd_dice_transaction_write(struct snd_dice *dice,
37 enum snd_dice_addr_type type,
38 unsigned int offset, void *buf, unsigned int len)
40 return snd_fw_transaction(dice->unit,
41 (len == 4) ? TCODE_WRITE_QUADLET_REQUEST :
42 TCODE_WRITE_BLOCK_REQUEST,
43 get_subaddr(dice, type, offset), buf, len, 0);
46 int snd_dice_transaction_read(struct snd_dice *dice,
47 enum snd_dice_addr_type type, unsigned int offset,
48 void *buf, unsigned int len)
50 return snd_fw_transaction(dice->unit,
51 (len == 4) ? TCODE_READ_QUADLET_REQUEST :
52 TCODE_READ_BLOCK_REQUEST,
53 get_subaddr(dice, type, offset), buf, len, 0);
56 static unsigned int get_clock_info(struct snd_dice *dice, __be32 *info)
58 return snd_dice_transaction_read_global(dice, GLOBAL_CLOCK_SELECT,
62 int snd_dice_transaction_get_clock_source(struct snd_dice *dice,
68 err = get_clock_info(dice, &info);
70 *source = be32_to_cpu(info) & CLOCK_SOURCE_MASK;
75 int snd_dice_transaction_get_rate(struct snd_dice *dice, unsigned int *rate)
81 err = get_clock_info(dice, &info);
85 index = (be32_to_cpu(info) & CLOCK_RATE_MASK) >> CLOCK_RATE_SHIFT;
86 if (index >= SND_DICE_RATES_COUNT) {
91 *rate = snd_dice_rates[index];
96 int snd_dice_transaction_set_enable(struct snd_dice *dice)
101 if (dice->global_enabled)
104 value = cpu_to_be32(1);
105 err = snd_fw_transaction(dice->unit, TCODE_WRITE_QUADLET_REQUEST,
106 get_subaddr(dice, SND_DICE_ADDR_TYPE_GLOBAL,
109 FW_FIXED_GENERATION | dice->owner_generation);
113 dice->global_enabled = true;
118 void snd_dice_transaction_clear_enable(struct snd_dice *dice)
123 snd_fw_transaction(dice->unit, TCODE_WRITE_QUADLET_REQUEST,
124 get_subaddr(dice, SND_DICE_ADDR_TYPE_GLOBAL,
126 &value, 4, FW_QUIET |
127 FW_FIXED_GENERATION | dice->owner_generation);
129 dice->global_enabled = false;
132 static void dice_notification(struct fw_card *card, struct fw_request *request,
133 int tcode, int destination, int source,
134 int generation, unsigned long long offset,
135 void *data, size_t length, void *callback_data)
137 struct snd_dice *dice = callback_data;
141 if (tcode != TCODE_WRITE_QUADLET_REQUEST) {
142 fw_send_response(card, request, RCODE_TYPE_ERROR);
145 if ((offset & 3) != 0) {
146 fw_send_response(card, request, RCODE_ADDRESS_ERROR);
150 bits = be32_to_cpup(data);
152 spin_lock_irqsave(&dice->lock, flags);
153 dice->notification_bits |= bits;
154 spin_unlock_irqrestore(&dice->lock, flags);
156 fw_send_response(card, request, RCODE_COMPLETE);
158 if (bits & NOTIFY_LOCK_CHG)
159 complete(&dice->clock_accepted);
160 wake_up(&dice->hwdep_wait);
163 static int register_notification_address(struct snd_dice *dice, bool retry)
165 struct fw_device *device = fw_parent_device(dice->unit);
167 unsigned int retries;
170 retries = (retry) ? 3 : 0;
172 buffer = kmalloc(2 * 8, GFP_KERNEL);
177 buffer[0] = cpu_to_be64(OWNER_NO_OWNER);
178 buffer[1] = cpu_to_be64(
179 ((u64)device->card->node_id << OWNER_NODE_SHIFT) |
180 dice->notification_handler.offset);
182 dice->owner_generation = device->generation;
183 smp_rmb(); /* node_id vs. generation */
184 err = snd_fw_transaction(dice->unit, TCODE_LOCK_COMPARE_SWAP,
186 SND_DICE_ADDR_TYPE_GLOBAL,
189 FW_FIXED_GENERATION |
190 dice->owner_generation);
193 if (buffer[0] == cpu_to_be64(OWNER_NO_OWNER))
195 /* The address seems to be already registered. */
196 if (buffer[0] == buffer[1])
199 dev_err(&dice->unit->device,
200 "device is already in use\n");
203 if (err != -EAGAIN || retries-- > 0)
212 dice->owner_generation = -1;
217 static void unregister_notification_address(struct snd_dice *dice)
219 struct fw_device *device = fw_parent_device(dice->unit);
222 buffer = kmalloc(2 * 8, GFP_KERNEL);
226 buffer[0] = cpu_to_be64(
227 ((u64)device->card->node_id << OWNER_NODE_SHIFT) |
228 dice->notification_handler.offset);
229 buffer[1] = cpu_to_be64(OWNER_NO_OWNER);
230 snd_fw_transaction(dice->unit, TCODE_LOCK_COMPARE_SWAP,
231 get_subaddr(dice, SND_DICE_ADDR_TYPE_GLOBAL,
233 buffer, 2 * 8, FW_QUIET |
234 FW_FIXED_GENERATION | dice->owner_generation);
238 dice->owner_generation = -1;
241 void snd_dice_transaction_destroy(struct snd_dice *dice)
243 struct fw_address_handler *handler = &dice->notification_handler;
245 if (handler->callback_data == NULL)
248 unregister_notification_address(dice);
250 fw_core_remove_address_handler(handler);
251 handler->callback_data = NULL;
254 int snd_dice_transaction_reinit(struct snd_dice *dice)
256 struct fw_address_handler *handler = &dice->notification_handler;
258 if (handler->callback_data == NULL)
261 return register_notification_address(dice, false);
264 static int get_subaddrs(struct snd_dice *dice)
266 static const int min_values[10] = {
279 pointers = kmalloc_array(ARRAY_SIZE(min_values), sizeof(__be32),
281 if (pointers == NULL)
285 * Check that the sub address spaces exist and are located inside the
286 * private address space. The minimum values are chosen so that all
287 * minimally required registers are included.
289 err = snd_fw_transaction(dice->unit, TCODE_READ_BLOCK_REQUEST,
290 DICE_PRIVATE_SPACE, pointers,
291 sizeof(__be32) * ARRAY_SIZE(min_values), 0);
295 for (i = 0; i < ARRAY_SIZE(min_values); ++i) {
296 data = be32_to_cpu(pointers[i]);
297 if (data < min_values[i] || data >= 0x40000) {
303 if (be32_to_cpu(pointers[1]) > 0x18) {
305 * Check that the implemented DICE driver specification major
306 * version number matches.
308 err = snd_fw_transaction(dice->unit, TCODE_READ_QUADLET_REQUEST,
310 be32_to_cpu(pointers[0]) * 4 + GLOBAL_VERSION,
311 &version, sizeof(version), 0);
315 if ((version & cpu_to_be32(0xff000000)) !=
316 cpu_to_be32(0x01000000)) {
317 dev_err(&dice->unit->device,
318 "unknown DICE version: 0x%08x\n",
319 be32_to_cpu(version));
325 dice->clock_caps = 1;
328 dice->global_offset = be32_to_cpu(pointers[0]) * 4;
329 dice->tx_offset = be32_to_cpu(pointers[2]) * 4;
330 dice->rx_offset = be32_to_cpu(pointers[4]) * 4;
332 /* Old firmware doesn't support these fields. */
334 dice->sync_offset = be32_to_cpu(pointers[6]) * 4;
336 dice->rsrv_offset = be32_to_cpu(pointers[8]) * 4;
342 int snd_dice_transaction_init(struct snd_dice *dice)
344 struct fw_address_handler *handler = &dice->notification_handler;
347 err = get_subaddrs(dice);
351 /* Allocation callback in address space over host controller */
353 handler->address_callback = dice_notification;
354 handler->callback_data = dice;
355 err = fw_core_add_address_handler(handler, &fw_high_memory_region);
357 handler->callback_data = NULL;
361 /* Register the address space */
362 err = register_notification_address(dice, true);
364 fw_core_remove_address_handler(handler);
365 handler->callback_data = NULL;