]> bbs.cooldavid.org Git - net-next-2.6.git/blame - drivers/firewire/fw-ohci.c
firewire: fw-ohci: flush MMIO write before msleep
[net-next-2.6.git] / drivers / firewire / fw-ohci.c
CommitLineData
c781c06d
KH
1/*
2 * Driver for OHCI 1394 controllers
ed568912 3 *
ed568912
KH
4 * Copyright (C) 2003-2006 Kristian Hoegsberg <krh@bitplanet.net>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 */
20
21#include <linux/kernel.h>
22#include <linux/module.h>
23#include <linux/init.h>
24#include <linux/interrupt.h>
25#include <linux/pci.h>
26#include <linux/delay.h>
27#include <linux/poll.h>
cf3e72fd 28#include <linux/dma-mapping.h>
faa2fb4e 29#include <linux/mm.h>
cf3e72fd 30
ed568912
KH
31#include <asm/uaccess.h>
32#include <asm/semaphore.h>
33
34#include "fw-transaction.h"
35#include "fw-ohci.h"
36
a77754a7
KH
37#define DESCRIPTOR_OUTPUT_MORE 0
38#define DESCRIPTOR_OUTPUT_LAST (1 << 12)
39#define DESCRIPTOR_INPUT_MORE (2 << 12)
40#define DESCRIPTOR_INPUT_LAST (3 << 12)
41#define DESCRIPTOR_STATUS (1 << 11)
42#define DESCRIPTOR_KEY_IMMEDIATE (2 << 8)
43#define DESCRIPTOR_PING (1 << 7)
44#define DESCRIPTOR_YY (1 << 6)
45#define DESCRIPTOR_NO_IRQ (0 << 4)
46#define DESCRIPTOR_IRQ_ERROR (1 << 4)
47#define DESCRIPTOR_IRQ_ALWAYS (3 << 4)
48#define DESCRIPTOR_BRANCH_ALWAYS (3 << 2)
49#define DESCRIPTOR_WAIT (3 << 0)
ed568912
KH
50
51struct descriptor {
52 __le16 req_count;
53 __le16 control;
54 __le32 data_address;
55 __le32 branch_address;
56 __le16 res_count;
57 __le16 transfer_status;
58} __attribute__((aligned(16)));
59
295e3feb
KH
60struct db_descriptor {
61 __le16 first_size;
62 __le16 control;
63 __le16 second_req_count;
64 __le16 first_req_count;
65 __le32 branch_address;
66 __le16 second_res_count;
67 __le16 first_res_count;
68 __le32 reserved0;
69 __le32 first_buffer;
70 __le32 second_buffer;
71 __le32 reserved1;
72} __attribute__((aligned(16)));
73
a77754a7
KH
74#define CONTROL_SET(regs) (regs)
75#define CONTROL_CLEAR(regs) ((regs) + 4)
76#define COMMAND_PTR(regs) ((regs) + 12)
77#define CONTEXT_MATCH(regs) ((regs) + 16)
72e318e0 78
32b46093 79struct ar_buffer {
ed568912 80 struct descriptor descriptor;
32b46093
KH
81 struct ar_buffer *next;
82 __le32 data[0];
83};
ed568912 84
32b46093
KH
85struct ar_context {
86 struct fw_ohci *ohci;
87 struct ar_buffer *current_buffer;
88 struct ar_buffer *last_buffer;
89 void *pointer;
72e318e0 90 u32 regs;
ed568912
KH
91 struct tasklet_struct tasklet;
92};
93
30200739
KH
94struct context;
95
96typedef int (*descriptor_callback_t)(struct context *ctx,
97 struct descriptor *d,
98 struct descriptor *last);
99struct context {
373b2edd 100 struct fw_ohci *ohci;
30200739 101 u32 regs;
373b2edd 102
30200739
KH
103 struct descriptor *buffer;
104 dma_addr_t buffer_bus;
105 size_t buffer_size;
106 struct descriptor *head_descriptor;
107 struct descriptor *tail_descriptor;
108 struct descriptor *tail_descriptor_last;
109 struct descriptor *prev_descriptor;
110
111 descriptor_callback_t callback;
112
373b2edd 113 struct tasklet_struct tasklet;
30200739 114};
30200739 115
a77754a7
KH
116#define IT_HEADER_SY(v) ((v) << 0)
117#define IT_HEADER_TCODE(v) ((v) << 4)
118#define IT_HEADER_CHANNEL(v) ((v) << 8)
119#define IT_HEADER_TAG(v) ((v) << 14)
120#define IT_HEADER_SPEED(v) ((v) << 16)
121#define IT_HEADER_DATA_LENGTH(v) ((v) << 16)
ed568912
KH
122
123struct iso_context {
124 struct fw_iso_context base;
30200739 125 struct context context;
9b32d5f3
KH
126 void *header;
127 size_t header_length;
ed568912
KH
128};
129
130#define CONFIG_ROM_SIZE 1024
131
132struct fw_ohci {
133 struct fw_card card;
134
e364cf4e 135 u32 version;
ed568912
KH
136 __iomem char *registers;
137 dma_addr_t self_id_bus;
138 __le32 *self_id_cpu;
139 struct tasklet_struct bus_reset_tasklet;
e636fe25 140 int node_id;
ed568912
KH
141 int generation;
142 int request_generation;
d60d7f1d 143 u32 bus_seconds;
ed568912 144
c781c06d
KH
145 /*
146 * Spinlock for accessing fw_ohci data. Never call out of
147 * this driver with this lock held.
148 */
ed568912
KH
149 spinlock_t lock;
150 u32 self_id_buffer[512];
151
152 /* Config rom buffers */
153 __be32 *config_rom;
154 dma_addr_t config_rom_bus;
155 __be32 *next_config_rom;
156 dma_addr_t next_config_rom_bus;
157 u32 next_header;
158
159 struct ar_context ar_request_ctx;
160 struct ar_context ar_response_ctx;
f319b6a0
KH
161 struct context at_request_ctx;
162 struct context at_response_ctx;
ed568912
KH
163
164 u32 it_context_mask;
165 struct iso_context *it_context_list;
166 u32 ir_context_mask;
167 struct iso_context *ir_context_list;
168};
169
95688e97 170static inline struct fw_ohci *fw_ohci(struct fw_card *card)
ed568912
KH
171{
172 return container_of(card, struct fw_ohci, card);
173}
174
295e3feb
KH
175#define IT_CONTEXT_CYCLE_MATCH_ENABLE 0x80000000
176#define IR_CONTEXT_BUFFER_FILL 0x80000000
177#define IR_CONTEXT_ISOCH_HEADER 0x40000000
178#define IR_CONTEXT_CYCLE_MATCH_ENABLE 0x20000000
179#define IR_CONTEXT_MULTI_CHANNEL_MODE 0x10000000
180#define IR_CONTEXT_DUAL_BUFFER_MODE 0x08000000
ed568912
KH
181
182#define CONTEXT_RUN 0x8000
183#define CONTEXT_WAKE 0x1000
184#define CONTEXT_DEAD 0x0800
185#define CONTEXT_ACTIVE 0x0400
186
187#define OHCI1394_MAX_AT_REQ_RETRIES 0x2
188#define OHCI1394_MAX_AT_RESP_RETRIES 0x2
189#define OHCI1394_MAX_PHYS_RESP_RETRIES 0x8
190
191#define FW_OHCI_MAJOR 240
192#define OHCI1394_REGISTER_SIZE 0x800
193#define OHCI_LOOP_COUNT 500
194#define OHCI1394_PCI_HCI_Control 0x40
195#define SELF_ID_BUF_SIZE 0x800
32b46093 196#define OHCI_TCODE_PHY_PACKET 0x0e
e364cf4e 197#define OHCI_VERSION_1_1 0x010010
f319b6a0
KH
198#define ISO_BUFFER_SIZE (64 * 1024)
199#define AT_BUFFER_SIZE 4096
0edeefd9 200
ed568912
KH
201static char ohci_driver_name[] = KBUILD_MODNAME;
202
95688e97 203static inline void reg_write(const struct fw_ohci *ohci, int offset, u32 data)
ed568912
KH
204{
205 writel(data, ohci->registers + offset);
206}
207
95688e97 208static inline u32 reg_read(const struct fw_ohci *ohci, int offset)
ed568912
KH
209{
210 return readl(ohci->registers + offset);
211}
212
95688e97 213static inline void flush_writes(const struct fw_ohci *ohci)
ed568912
KH
214{
215 /* Do a dummy read to flush writes. */
216 reg_read(ohci, OHCI1394_Version);
217}
218
219static int
220ohci_update_phy_reg(struct fw_card *card, int addr,
221 int clear_bits, int set_bits)
222{
223 struct fw_ohci *ohci = fw_ohci(card);
224 u32 val, old;
225
226 reg_write(ohci, OHCI1394_PhyControl, OHCI1394_PhyControl_Read(addr));
362e901c 227 flush_writes(ohci);
ed568912
KH
228 msleep(2);
229 val = reg_read(ohci, OHCI1394_PhyControl);
230 if ((val & OHCI1394_PhyControl_ReadDone) == 0) {
231 fw_error("failed to set phy reg bits.\n");
232 return -EBUSY;
233 }
234
235 old = OHCI1394_PhyControl_ReadData(val);
236 old = (old & ~clear_bits) | set_bits;
237 reg_write(ohci, OHCI1394_PhyControl,
238 OHCI1394_PhyControl_Write(addr, old));
239
240 return 0;
241}
242
32b46093 243static int ar_context_add_page(struct ar_context *ctx)
ed568912 244{
32b46093
KH
245 struct device *dev = ctx->ohci->card.device;
246 struct ar_buffer *ab;
247 dma_addr_t ab_bus;
248 size_t offset;
249
250 ab = (struct ar_buffer *) __get_free_page(GFP_ATOMIC);
251 if (ab == NULL)
252 return -ENOMEM;
253
254 ab_bus = dma_map_single(dev, ab, PAGE_SIZE, DMA_BIDIRECTIONAL);
255 if (dma_mapping_error(ab_bus)) {
256 free_page((unsigned long) ab);
257 return -ENOMEM;
258 }
259
2d826cc5 260 memset(&ab->descriptor, 0, sizeof(ab->descriptor));
a77754a7
KH
261 ab->descriptor.control = cpu_to_le16(DESCRIPTOR_INPUT_MORE |
262 DESCRIPTOR_STATUS |
263 DESCRIPTOR_BRANCH_ALWAYS);
32b46093
KH
264 offset = offsetof(struct ar_buffer, data);
265 ab->descriptor.req_count = cpu_to_le16(PAGE_SIZE - offset);
266 ab->descriptor.data_address = cpu_to_le32(ab_bus + offset);
267 ab->descriptor.res_count = cpu_to_le16(PAGE_SIZE - offset);
268 ab->descriptor.branch_address = 0;
269
270 dma_sync_single_for_device(dev, ab_bus, PAGE_SIZE, DMA_BIDIRECTIONAL);
271
ec839e43 272 ctx->last_buffer->descriptor.branch_address = cpu_to_le32(ab_bus | 1);
32b46093
KH
273 ctx->last_buffer->next = ab;
274 ctx->last_buffer = ab;
275
a77754a7 276 reg_write(ctx->ohci, CONTROL_SET(ctx->regs), CONTEXT_WAKE);
ed568912 277 flush_writes(ctx->ohci);
32b46093
KH
278
279 return 0;
ed568912
KH
280}
281
32b46093 282static __le32 *handle_ar_packet(struct ar_context *ctx, __le32 *buffer)
ed568912 283{
ed568912 284 struct fw_ohci *ohci = ctx->ohci;
2639a6fb
KH
285 struct fw_packet p;
286 u32 status, length, tcode;
2639a6fb 287
32b46093
KH
288 p.header[0] = le32_to_cpu(buffer[0]);
289 p.header[1] = le32_to_cpu(buffer[1]);
290 p.header[2] = le32_to_cpu(buffer[2]);
2639a6fb
KH
291
292 tcode = (p.header[0] >> 4) & 0x0f;
293 switch (tcode) {
294 case TCODE_WRITE_QUADLET_REQUEST:
295 case TCODE_READ_QUADLET_RESPONSE:
32b46093 296 p.header[3] = (__force __u32) buffer[3];
2639a6fb 297 p.header_length = 16;
32b46093 298 p.payload_length = 0;
2639a6fb
KH
299 break;
300
2639a6fb 301 case TCODE_READ_BLOCK_REQUEST :
32b46093
KH
302 p.header[3] = le32_to_cpu(buffer[3]);
303 p.header_length = 16;
304 p.payload_length = 0;
305 break;
306
307 case TCODE_WRITE_BLOCK_REQUEST:
2639a6fb
KH
308 case TCODE_READ_BLOCK_RESPONSE:
309 case TCODE_LOCK_REQUEST:
310 case TCODE_LOCK_RESPONSE:
32b46093 311 p.header[3] = le32_to_cpu(buffer[3]);
2639a6fb 312 p.header_length = 16;
32b46093 313 p.payload_length = p.header[3] >> 16;
2639a6fb
KH
314 break;
315
316 case TCODE_WRITE_RESPONSE:
317 case TCODE_READ_QUADLET_REQUEST:
32b46093 318 case OHCI_TCODE_PHY_PACKET:
2639a6fb 319 p.header_length = 12;
32b46093 320 p.payload_length = 0;
2639a6fb
KH
321 break;
322 }
ed568912 323
32b46093
KH
324 p.payload = (void *) buffer + p.header_length;
325
326 /* FIXME: What to do about evt_* errors? */
327 length = (p.header_length + p.payload_length + 3) / 4;
328 status = le32_to_cpu(buffer[length]);
329
330 p.ack = ((status >> 16) & 0x1f) - 16;
331 p.speed = (status >> 21) & 0x7;
332 p.timestamp = status & 0xffff;
333 p.generation = ohci->request_generation;
ed568912 334
c781c06d
KH
335 /*
336 * The OHCI bus reset handler synthesizes a phy packet with
ed568912
KH
337 * the new generation number when a bus reset happens (see
338 * section 8.4.2.3). This helps us determine when a request
339 * was received and make sure we send the response in the same
340 * generation. We only need this for requests; for responses
341 * we use the unique tlabel for finding the matching
c781c06d
KH
342 * request.
343 */
ed568912 344
2639a6fb 345 if (p.ack + 16 == 0x09)
32b46093 346 ohci->request_generation = (buffer[2] >> 16) & 0xff;
ed568912 347 else if (ctx == &ohci->ar_request_ctx)
2639a6fb 348 fw_core_handle_request(&ohci->card, &p);
ed568912 349 else
2639a6fb 350 fw_core_handle_response(&ohci->card, &p);
ed568912 351
32b46093
KH
352 return buffer + length + 1;
353}
ed568912 354
32b46093
KH
355static void ar_context_tasklet(unsigned long data)
356{
357 struct ar_context *ctx = (struct ar_context *)data;
358 struct fw_ohci *ohci = ctx->ohci;
359 struct ar_buffer *ab;
360 struct descriptor *d;
361 void *buffer, *end;
362
363 ab = ctx->current_buffer;
364 d = &ab->descriptor;
365
366 if (d->res_count == 0) {
367 size_t size, rest, offset;
368
c781c06d
KH
369 /*
370 * This descriptor is finished and we may have a
32b46093 371 * packet split across this and the next buffer. We
c781c06d
KH
372 * reuse the page for reassembling the split packet.
373 */
32b46093
KH
374
375 offset = offsetof(struct ar_buffer, data);
376 dma_unmap_single(ohci->card.device,
0a9972ba
SR
377 le32_to_cpu(ab->descriptor.data_address) - offset,
378 PAGE_SIZE, DMA_BIDIRECTIONAL);
32b46093
KH
379
380 buffer = ab;
381 ab = ab->next;
382 d = &ab->descriptor;
383 size = buffer + PAGE_SIZE - ctx->pointer;
384 rest = le16_to_cpu(d->req_count) - le16_to_cpu(d->res_count);
385 memmove(buffer, ctx->pointer, size);
386 memcpy(buffer + size, ab->data, rest);
387 ctx->current_buffer = ab;
388 ctx->pointer = (void *) ab->data + rest;
389 end = buffer + size + rest;
390
391 while (buffer < end)
392 buffer = handle_ar_packet(ctx, buffer);
393
394 free_page((unsigned long)buffer);
395 ar_context_add_page(ctx);
396 } else {
397 buffer = ctx->pointer;
398 ctx->pointer = end =
399 (void *) ab + PAGE_SIZE - le16_to_cpu(d->res_count);
400
401 while (buffer < end)
402 buffer = handle_ar_packet(ctx, buffer);
403 }
ed568912
KH
404}
405
406static int
72e318e0 407ar_context_init(struct ar_context *ctx, struct fw_ohci *ohci, u32 regs)
ed568912 408{
32b46093 409 struct ar_buffer ab;
ed568912 410
72e318e0
KH
411 ctx->regs = regs;
412 ctx->ohci = ohci;
413 ctx->last_buffer = &ab;
ed568912
KH
414 tasklet_init(&ctx->tasklet, ar_context_tasklet, (unsigned long)ctx);
415
32b46093
KH
416 ar_context_add_page(ctx);
417 ar_context_add_page(ctx);
418 ctx->current_buffer = ab.next;
419 ctx->pointer = ctx->current_buffer->data;
420
2aef469a
KH
421 return 0;
422}
423
424static void ar_context_run(struct ar_context *ctx)
425{
426 struct ar_buffer *ab = ctx->current_buffer;
427 dma_addr_t ab_bus;
428 size_t offset;
429
430 offset = offsetof(struct ar_buffer, data);
0a9972ba 431 ab_bus = le32_to_cpu(ab->descriptor.data_address) - offset;
2aef469a
KH
432
433 reg_write(ctx->ohci, COMMAND_PTR(ctx->regs), ab_bus | 1);
a77754a7 434 reg_write(ctx->ohci, CONTROL_SET(ctx->regs), CONTEXT_RUN);
32b46093 435 flush_writes(ctx->ohci);
ed568912 436}
373b2edd 437
30200739
KH
438static void context_tasklet(unsigned long data)
439{
440 struct context *ctx = (struct context *) data;
441 struct fw_ohci *ohci = ctx->ohci;
442 struct descriptor *d, *last;
443 u32 address;
444 int z;
445
446 dma_sync_single_for_cpu(ohci->card.device, ctx->buffer_bus,
447 ctx->buffer_size, DMA_TO_DEVICE);
448
449 d = ctx->tail_descriptor;
450 last = ctx->tail_descriptor_last;
451
452 while (last->branch_address != 0) {
453 address = le32_to_cpu(last->branch_address);
454 z = address & 0xf;
2d826cc5 455 d = ctx->buffer + (address - ctx->buffer_bus) / sizeof(*d);
30200739
KH
456 last = (z == 2) ? d : d + z - 1;
457
458 if (!ctx->callback(ctx, d, last))
459 break;
460
461 ctx->tail_descriptor = d;
462 ctx->tail_descriptor_last = last;
463 }
464}
465
466static int
467context_init(struct context *ctx, struct fw_ohci *ohci,
468 size_t buffer_size, u32 regs,
469 descriptor_callback_t callback)
470{
471 ctx->ohci = ohci;
472 ctx->regs = regs;
473 ctx->buffer_size = buffer_size;
474 ctx->buffer = kmalloc(buffer_size, GFP_KERNEL);
475 if (ctx->buffer == NULL)
476 return -ENOMEM;
477
478 tasklet_init(&ctx->tasklet, context_tasklet, (unsigned long)ctx);
479 ctx->callback = callback;
480
481 ctx->buffer_bus =
482 dma_map_single(ohci->card.device, ctx->buffer,
483 buffer_size, DMA_TO_DEVICE);
484 if (dma_mapping_error(ctx->buffer_bus)) {
485 kfree(ctx->buffer);
486 return -ENOMEM;
487 }
488
489 ctx->head_descriptor = ctx->buffer;
490 ctx->prev_descriptor = ctx->buffer;
491 ctx->tail_descriptor = ctx->buffer;
492 ctx->tail_descriptor_last = ctx->buffer;
493
c781c06d
KH
494 /*
495 * We put a dummy descriptor in the buffer that has a NULL
30200739
KH
496 * branch address and looks like it's been sent. That way we
497 * have a descriptor to append DMA programs to. Also, the
498 * ring buffer invariant is that it always has at least one
c781c06d
KH
499 * element so that head == tail means buffer full.
500 */
30200739 501
2d826cc5 502 memset(ctx->head_descriptor, 0, sizeof(*ctx->head_descriptor));
a77754a7 503 ctx->head_descriptor->control = cpu_to_le16(DESCRIPTOR_OUTPUT_LAST);
30200739
KH
504 ctx->head_descriptor->transfer_status = cpu_to_le16(0x8011);
505 ctx->head_descriptor++;
506
507 return 0;
508}
509
9b32d5f3 510static void
30200739
KH
511context_release(struct context *ctx)
512{
513 struct fw_card *card = &ctx->ohci->card;
514
515 dma_unmap_single(card->device, ctx->buffer_bus,
516 ctx->buffer_size, DMA_TO_DEVICE);
517 kfree(ctx->buffer);
518}
519
520static struct descriptor *
521context_get_descriptors(struct context *ctx, int z, dma_addr_t *d_bus)
522{
523 struct descriptor *d, *tail, *end;
524
525 d = ctx->head_descriptor;
526 tail = ctx->tail_descriptor;
2d826cc5 527 end = ctx->buffer + ctx->buffer_size / sizeof(*d);
30200739
KH
528
529 if (d + z <= tail) {
530 goto has_space;
531 } else if (d > tail && d + z <= end) {
532 goto has_space;
533 } else if (d > tail && ctx->buffer + z <= tail) {
534 d = ctx->buffer;
535 goto has_space;
536 }
537
538 return NULL;
539
540 has_space:
2d826cc5
KH
541 memset(d, 0, z * sizeof(*d));
542 *d_bus = ctx->buffer_bus + (d - ctx->buffer) * sizeof(*d);
30200739
KH
543
544 return d;
545}
546
295e3feb 547static void context_run(struct context *ctx, u32 extra)
30200739
KH
548{
549 struct fw_ohci *ohci = ctx->ohci;
550
a77754a7 551 reg_write(ohci, COMMAND_PTR(ctx->regs),
30200739 552 le32_to_cpu(ctx->tail_descriptor_last->branch_address));
a77754a7
KH
553 reg_write(ohci, CONTROL_CLEAR(ctx->regs), ~0);
554 reg_write(ohci, CONTROL_SET(ctx->regs), CONTEXT_RUN | extra);
30200739
KH
555 flush_writes(ohci);
556}
557
558static void context_append(struct context *ctx,
559 struct descriptor *d, int z, int extra)
560{
561 dma_addr_t d_bus;
562
2d826cc5 563 d_bus = ctx->buffer_bus + (d - ctx->buffer) * sizeof(*d);
30200739
KH
564
565 ctx->head_descriptor = d + z + extra;
566 ctx->prev_descriptor->branch_address = cpu_to_le32(d_bus | z);
567 ctx->prev_descriptor = z == 2 ? d : d + z - 1;
568
569 dma_sync_single_for_device(ctx->ohci->card.device, ctx->buffer_bus,
570 ctx->buffer_size, DMA_TO_DEVICE);
571
a77754a7 572 reg_write(ctx->ohci, CONTROL_SET(ctx->regs), CONTEXT_WAKE);
30200739
KH
573 flush_writes(ctx->ohci);
574}
575
576static void context_stop(struct context *ctx)
577{
578 u32 reg;
b8295668 579 int i;
30200739 580
a77754a7 581 reg_write(ctx->ohci, CONTROL_CLEAR(ctx->regs), CONTEXT_RUN);
b8295668 582 flush_writes(ctx->ohci);
30200739 583
b8295668 584 for (i = 0; i < 10; i++) {
a77754a7 585 reg = reg_read(ctx->ohci, CONTROL_SET(ctx->regs));
b8295668
KH
586 if ((reg & CONTEXT_ACTIVE) == 0)
587 break;
588
589 fw_notify("context_stop: still active (0x%08x)\n", reg);
590 msleep(1);
591 }
30200739 592}
ed568912 593
f319b6a0
KH
594struct driver_data {
595 struct fw_packet *packet;
596};
ed568912 597
c781c06d
KH
598/*
599 * This function apppends a packet to the DMA queue for transmission.
f319b6a0 600 * Must always be called with the ochi->lock held to ensure proper
c781c06d
KH
601 * generation handling and locking around packet queue manipulation.
602 */
f319b6a0
KH
603static int
604at_context_queue_packet(struct context *ctx, struct fw_packet *packet)
ed568912 605{
ed568912 606 struct fw_ohci *ohci = ctx->ohci;
f319b6a0
KH
607 dma_addr_t d_bus, payload_bus;
608 struct driver_data *driver_data;
609 struct descriptor *d, *last;
610 __le32 *header;
ed568912 611 int z, tcode;
f319b6a0 612 u32 reg;
ed568912 613
f319b6a0
KH
614 d = context_get_descriptors(ctx, 4, &d_bus);
615 if (d == NULL) {
616 packet->ack = RCODE_SEND_ERROR;
617 return -1;
ed568912
KH
618 }
619
a77754a7 620 d[0].control = cpu_to_le16(DESCRIPTOR_KEY_IMMEDIATE);
f319b6a0
KH
621 d[0].res_count = cpu_to_le16(packet->timestamp);
622
c781c06d
KH
623 /*
624 * The DMA format for asyncronous link packets is different
ed568912
KH
625 * from the IEEE1394 layout, so shift the fields around
626 * accordingly. If header_length is 8, it's a PHY packet, to
c781c06d
KH
627 * which we need to prepend an extra quadlet.
628 */
f319b6a0
KH
629
630 header = (__le32 *) &d[1];
ed568912 631 if (packet->header_length > 8) {
f319b6a0
KH
632 header[0] = cpu_to_le32((packet->header[0] & 0xffff) |
633 (packet->speed << 16));
634 header[1] = cpu_to_le32((packet->header[1] & 0xffff) |
635 (packet->header[0] & 0xffff0000));
636 header[2] = cpu_to_le32(packet->header[2]);
ed568912
KH
637
638 tcode = (packet->header[0] >> 4) & 0x0f;
639 if (TCODE_IS_BLOCK_PACKET(tcode))
f319b6a0 640 header[3] = cpu_to_le32(packet->header[3]);
ed568912 641 else
f319b6a0
KH
642 header[3] = (__force __le32) packet->header[3];
643
644 d[0].req_count = cpu_to_le16(packet->header_length);
ed568912 645 } else {
f319b6a0
KH
646 header[0] = cpu_to_le32((OHCI1394_phy_tcode << 4) |
647 (packet->speed << 16));
648 header[1] = cpu_to_le32(packet->header[0]);
649 header[2] = cpu_to_le32(packet->header[1]);
650 d[0].req_count = cpu_to_le16(12);
ed568912
KH
651 }
652
f319b6a0
KH
653 driver_data = (struct driver_data *) &d[3];
654 driver_data->packet = packet;
20d11673 655 packet->driver_data = driver_data;
f319b6a0
KH
656
657 if (packet->payload_length > 0) {
658 payload_bus =
659 dma_map_single(ohci->card.device, packet->payload,
660 packet->payload_length, DMA_TO_DEVICE);
661 if (dma_mapping_error(payload_bus)) {
662 packet->ack = RCODE_SEND_ERROR;
663 return -1;
664 }
665
666 d[2].req_count = cpu_to_le16(packet->payload_length);
667 d[2].data_address = cpu_to_le32(payload_bus);
668 last = &d[2];
669 z = 3;
ed568912 670 } else {
f319b6a0
KH
671 last = &d[0];
672 z = 2;
ed568912 673 }
ed568912 674
a77754a7
KH
675 last->control |= cpu_to_le16(DESCRIPTOR_OUTPUT_LAST |
676 DESCRIPTOR_IRQ_ALWAYS |
677 DESCRIPTOR_BRANCH_ALWAYS);
ed568912 678
f319b6a0
KH
679 /* FIXME: Document how the locking works. */
680 if (ohci->generation != packet->generation) {
681 packet->ack = RCODE_GENERATION;
682 return -1;
683 }
684
685 context_append(ctx, d, z, 4 - z);
ed568912 686
f319b6a0 687 /* If the context isn't already running, start it up. */
a77754a7 688 reg = reg_read(ctx->ohci, CONTROL_SET(ctx->regs));
053b3080 689 if ((reg & CONTEXT_RUN) == 0)
f319b6a0
KH
690 context_run(ctx, 0);
691
692 return 0;
ed568912
KH
693}
694
f319b6a0
KH
695static int handle_at_packet(struct context *context,
696 struct descriptor *d,
697 struct descriptor *last)
ed568912 698{
f319b6a0 699 struct driver_data *driver_data;
ed568912 700 struct fw_packet *packet;
f319b6a0
KH
701 struct fw_ohci *ohci = context->ohci;
702 dma_addr_t payload_bus;
ed568912
KH
703 int evt;
704
f319b6a0
KH
705 if (last->transfer_status == 0)
706 /* This descriptor isn't done yet, stop iteration. */
707 return 0;
ed568912 708
f319b6a0
KH
709 driver_data = (struct driver_data *) &d[3];
710 packet = driver_data->packet;
711 if (packet == NULL)
712 /* This packet was cancelled, just continue. */
713 return 1;
730c32f5 714
f319b6a0
KH
715 payload_bus = le32_to_cpu(last->data_address);
716 if (payload_bus != 0)
717 dma_unmap_single(ohci->card.device, payload_bus,
ed568912 718 packet->payload_length, DMA_TO_DEVICE);
ed568912 719
f319b6a0
KH
720 evt = le16_to_cpu(last->transfer_status) & 0x1f;
721 packet->timestamp = le16_to_cpu(last->res_count);
ed568912 722
f319b6a0
KH
723 switch (evt) {
724 case OHCI1394_evt_timeout:
725 /* Async response transmit timed out. */
726 packet->ack = RCODE_CANCELLED;
727 break;
ed568912 728
f319b6a0 729 case OHCI1394_evt_flushed:
c781c06d
KH
730 /*
731 * The packet was flushed should give same error as
732 * when we try to use a stale generation count.
733 */
f319b6a0
KH
734 packet->ack = RCODE_GENERATION;
735 break;
ed568912 736
f319b6a0 737 case OHCI1394_evt_missing_ack:
c781c06d
KH
738 /*
739 * Using a valid (current) generation count, but the
740 * node is not on the bus or not sending acks.
741 */
f319b6a0
KH
742 packet->ack = RCODE_NO_ACK;
743 break;
ed568912 744
f319b6a0
KH
745 case ACK_COMPLETE + 0x10:
746 case ACK_PENDING + 0x10:
747 case ACK_BUSY_X + 0x10:
748 case ACK_BUSY_A + 0x10:
749 case ACK_BUSY_B + 0x10:
750 case ACK_DATA_ERROR + 0x10:
751 case ACK_TYPE_ERROR + 0x10:
752 packet->ack = evt - 0x10;
753 break;
ed568912 754
f319b6a0
KH
755 default:
756 packet->ack = RCODE_SEND_ERROR;
757 break;
758 }
ed568912 759
f319b6a0 760 packet->callback(packet, &ohci->card, packet->ack);
ed568912 761
f319b6a0 762 return 1;
ed568912
KH
763}
764
a77754a7
KH
765#define HEADER_GET_DESTINATION(q) (((q) >> 16) & 0xffff)
766#define HEADER_GET_TCODE(q) (((q) >> 4) & 0x0f)
767#define HEADER_GET_OFFSET_HIGH(q) (((q) >> 0) & 0xffff)
768#define HEADER_GET_DATA_LENGTH(q) (((q) >> 16) & 0xffff)
769#define HEADER_GET_EXTENDED_TCODE(q) (((q) >> 0) & 0xffff)
93c4cceb
KH
770
771static void
772handle_local_rom(struct fw_ohci *ohci, struct fw_packet *packet, u32 csr)
773{
774 struct fw_packet response;
775 int tcode, length, i;
776
a77754a7 777 tcode = HEADER_GET_TCODE(packet->header[0]);
93c4cceb 778 if (TCODE_IS_BLOCK_PACKET(tcode))
a77754a7 779 length = HEADER_GET_DATA_LENGTH(packet->header[3]);
93c4cceb
KH
780 else
781 length = 4;
782
783 i = csr - CSR_CONFIG_ROM;
784 if (i + length > CONFIG_ROM_SIZE) {
785 fw_fill_response(&response, packet->header,
786 RCODE_ADDRESS_ERROR, NULL, 0);
787 } else if (!TCODE_IS_READ_REQUEST(tcode)) {
788 fw_fill_response(&response, packet->header,
789 RCODE_TYPE_ERROR, NULL, 0);
790 } else {
791 fw_fill_response(&response, packet->header, RCODE_COMPLETE,
792 (void *) ohci->config_rom + i, length);
793 }
794
795 fw_core_handle_response(&ohci->card, &response);
796}
797
798static void
799handle_local_lock(struct fw_ohci *ohci, struct fw_packet *packet, u32 csr)
800{
801 struct fw_packet response;
802 int tcode, length, ext_tcode, sel;
803 __be32 *payload, lock_old;
804 u32 lock_arg, lock_data;
805
a77754a7
KH
806 tcode = HEADER_GET_TCODE(packet->header[0]);
807 length = HEADER_GET_DATA_LENGTH(packet->header[3]);
93c4cceb 808 payload = packet->payload;
a77754a7 809 ext_tcode = HEADER_GET_EXTENDED_TCODE(packet->header[3]);
93c4cceb
KH
810
811 if (tcode == TCODE_LOCK_REQUEST &&
812 ext_tcode == EXTCODE_COMPARE_SWAP && length == 8) {
813 lock_arg = be32_to_cpu(payload[0]);
814 lock_data = be32_to_cpu(payload[1]);
815 } else if (tcode == TCODE_READ_QUADLET_REQUEST) {
816 lock_arg = 0;
817 lock_data = 0;
818 } else {
819 fw_fill_response(&response, packet->header,
820 RCODE_TYPE_ERROR, NULL, 0);
821 goto out;
822 }
823
824 sel = (csr - CSR_BUS_MANAGER_ID) / 4;
825 reg_write(ohci, OHCI1394_CSRData, lock_data);
826 reg_write(ohci, OHCI1394_CSRCompareData, lock_arg);
827 reg_write(ohci, OHCI1394_CSRControl, sel);
828
829 if (reg_read(ohci, OHCI1394_CSRControl) & 0x80000000)
830 lock_old = cpu_to_be32(reg_read(ohci, OHCI1394_CSRData));
831 else
832 fw_notify("swap not done yet\n");
833
834 fw_fill_response(&response, packet->header,
2d826cc5 835 RCODE_COMPLETE, &lock_old, sizeof(lock_old));
93c4cceb
KH
836 out:
837 fw_core_handle_response(&ohci->card, &response);
838}
839
840static void
f319b6a0 841handle_local_request(struct context *ctx, struct fw_packet *packet)
93c4cceb
KH
842{
843 u64 offset;
844 u32 csr;
845
473d28c7
KH
846 if (ctx == &ctx->ohci->at_request_ctx) {
847 packet->ack = ACK_PENDING;
848 packet->callback(packet, &ctx->ohci->card, packet->ack);
849 }
93c4cceb
KH
850
851 offset =
852 ((unsigned long long)
a77754a7 853 HEADER_GET_OFFSET_HIGH(packet->header[1]) << 32) |
93c4cceb
KH
854 packet->header[2];
855 csr = offset - CSR_REGISTER_BASE;
856
857 /* Handle config rom reads. */
858 if (csr >= CSR_CONFIG_ROM && csr < CSR_CONFIG_ROM_END)
859 handle_local_rom(ctx->ohci, packet, csr);
860 else switch (csr) {
861 case CSR_BUS_MANAGER_ID:
862 case CSR_BANDWIDTH_AVAILABLE:
863 case CSR_CHANNELS_AVAILABLE_HI:
864 case CSR_CHANNELS_AVAILABLE_LO:
865 handle_local_lock(ctx->ohci, packet, csr);
866 break;
867 default:
868 if (ctx == &ctx->ohci->at_request_ctx)
869 fw_core_handle_request(&ctx->ohci->card, packet);
870 else
871 fw_core_handle_response(&ctx->ohci->card, packet);
872 break;
873 }
473d28c7
KH
874
875 if (ctx == &ctx->ohci->at_response_ctx) {
876 packet->ack = ACK_COMPLETE;
877 packet->callback(packet, &ctx->ohci->card, packet->ack);
878 }
93c4cceb 879}
e636fe25 880
ed568912 881static void
f319b6a0 882at_context_transmit(struct context *ctx, struct fw_packet *packet)
ed568912 883{
ed568912 884 unsigned long flags;
f319b6a0 885 int retval;
ed568912
KH
886
887 spin_lock_irqsave(&ctx->ohci->lock, flags);
888
a77754a7 889 if (HEADER_GET_DESTINATION(packet->header[0]) == ctx->ohci->node_id &&
e636fe25 890 ctx->ohci->generation == packet->generation) {
93c4cceb
KH
891 spin_unlock_irqrestore(&ctx->ohci->lock, flags);
892 handle_local_request(ctx, packet);
893 return;
e636fe25 894 }
ed568912 895
f319b6a0 896 retval = at_context_queue_packet(ctx, packet);
ed568912
KH
897 spin_unlock_irqrestore(&ctx->ohci->lock, flags);
898
f319b6a0
KH
899 if (retval < 0)
900 packet->callback(packet, &ctx->ohci->card, packet->ack);
901
ed568912
KH
902}
903
904static void bus_reset_tasklet(unsigned long data)
905{
906 struct fw_ohci *ohci = (struct fw_ohci *)data;
e636fe25 907 int self_id_count, i, j, reg;
ed568912
KH
908 int generation, new_generation;
909 unsigned long flags;
910
911 reg = reg_read(ohci, OHCI1394_NodeID);
912 if (!(reg & OHCI1394_NodeID_idValid)) {
913 fw_error("node ID not valid, new bus reset in progress\n");
914 return;
915 }
e636fe25 916 ohci->node_id = reg & 0xffff;
ed568912 917
c781c06d
KH
918 /*
919 * The count in the SelfIDCount register is the number of
ed568912
KH
920 * bytes in the self ID receive buffer. Since we also receive
921 * the inverted quadlets and a header quadlet, we shift one
c781c06d
KH
922 * bit extra to get the actual number of self IDs.
923 */
ed568912
KH
924
925 self_id_count = (reg_read(ohci, OHCI1394_SelfIDCount) >> 3) & 0x3ff;
926 generation = (le32_to_cpu(ohci->self_id_cpu[0]) >> 16) & 0xff;
927
928 for (i = 1, j = 0; j < self_id_count; i += 2, j++) {
929 if (ohci->self_id_cpu[i] != ~ohci->self_id_cpu[i + 1])
930 fw_error("inconsistent self IDs\n");
931 ohci->self_id_buffer[j] = le32_to_cpu(ohci->self_id_cpu[i]);
932 }
933
c781c06d
KH
934 /*
935 * Check the consistency of the self IDs we just read. The
ed568912
KH
936 * problem we face is that a new bus reset can start while we
937 * read out the self IDs from the DMA buffer. If this happens,
938 * the DMA buffer will be overwritten with new self IDs and we
939 * will read out inconsistent data. The OHCI specification
940 * (section 11.2) recommends a technique similar to
941 * linux/seqlock.h, where we remember the generation of the
942 * self IDs in the buffer before reading them out and compare
943 * it to the current generation after reading them out. If
944 * the two generations match we know we have a consistent set
c781c06d
KH
945 * of self IDs.
946 */
ed568912
KH
947
948 new_generation = (reg_read(ohci, OHCI1394_SelfIDCount) >> 16) & 0xff;
949 if (new_generation != generation) {
950 fw_notify("recursive bus reset detected, "
951 "discarding self ids\n");
952 return;
953 }
954
955 /* FIXME: Document how the locking works. */
956 spin_lock_irqsave(&ohci->lock, flags);
957
958 ohci->generation = generation;
f319b6a0
KH
959 context_stop(&ohci->at_request_ctx);
960 context_stop(&ohci->at_response_ctx);
ed568912
KH
961 reg_write(ohci, OHCI1394_IntEventClear, OHCI1394_busReset);
962
c781c06d
KH
963 /*
964 * This next bit is unrelated to the AT context stuff but we
ed568912
KH
965 * have to do it under the spinlock also. If a new config rom
966 * was set up before this reset, the old one is now no longer
967 * in use and we can free it. Update the config rom pointers
968 * to point to the current config rom and clear the
c781c06d
KH
969 * next_config_rom pointer so a new udpate can take place.
970 */
ed568912
KH
971
972 if (ohci->next_config_rom != NULL) {
973 dma_free_coherent(ohci->card.device, CONFIG_ROM_SIZE,
974 ohci->config_rom, ohci->config_rom_bus);
975 ohci->config_rom = ohci->next_config_rom;
976 ohci->config_rom_bus = ohci->next_config_rom_bus;
977 ohci->next_config_rom = NULL;
978
c781c06d
KH
979 /*
980 * Restore config_rom image and manually update
ed568912
KH
981 * config_rom registers. Writing the header quadlet
982 * will indicate that the config rom is ready, so we
c781c06d
KH
983 * do that last.
984 */
ed568912
KH
985 reg_write(ohci, OHCI1394_BusOptions,
986 be32_to_cpu(ohci->config_rom[2]));
987 ohci->config_rom[0] = cpu_to_be32(ohci->next_header);
988 reg_write(ohci, OHCI1394_ConfigROMhdr, ohci->next_header);
989 }
990
991 spin_unlock_irqrestore(&ohci->lock, flags);
992
e636fe25 993 fw_core_handle_bus_reset(&ohci->card, ohci->node_id, generation,
ed568912
KH
994 self_id_count, ohci->self_id_buffer);
995}
996
997static irqreturn_t irq_handler(int irq, void *data)
998{
999 struct fw_ohci *ohci = data;
d60d7f1d 1000 u32 event, iso_event, cycle_time;
ed568912
KH
1001 int i;
1002
1003 event = reg_read(ohci, OHCI1394_IntEventClear);
1004
a515958d 1005 if (!event || !~event)
ed568912
KH
1006 return IRQ_NONE;
1007
1008 reg_write(ohci, OHCI1394_IntEventClear, event);
1009
1010 if (event & OHCI1394_selfIDComplete)
1011 tasklet_schedule(&ohci->bus_reset_tasklet);
1012
1013 if (event & OHCI1394_RQPkt)
1014 tasklet_schedule(&ohci->ar_request_ctx.tasklet);
1015
1016 if (event & OHCI1394_RSPkt)
1017 tasklet_schedule(&ohci->ar_response_ctx.tasklet);
1018
1019 if (event & OHCI1394_reqTxComplete)
1020 tasklet_schedule(&ohci->at_request_ctx.tasklet);
1021
1022 if (event & OHCI1394_respTxComplete)
1023 tasklet_schedule(&ohci->at_response_ctx.tasklet);
1024
c889475f 1025 iso_event = reg_read(ohci, OHCI1394_IsoRecvIntEventClear);
ed568912
KH
1026 reg_write(ohci, OHCI1394_IsoRecvIntEventClear, iso_event);
1027
1028 while (iso_event) {
1029 i = ffs(iso_event) - 1;
30200739 1030 tasklet_schedule(&ohci->ir_context_list[i].context.tasklet);
ed568912
KH
1031 iso_event &= ~(1 << i);
1032 }
1033
c889475f 1034 iso_event = reg_read(ohci, OHCI1394_IsoXmitIntEventClear);
ed568912
KH
1035 reg_write(ohci, OHCI1394_IsoXmitIntEventClear, iso_event);
1036
1037 while (iso_event) {
1038 i = ffs(iso_event) - 1;
30200739 1039 tasklet_schedule(&ohci->it_context_list[i].context.tasklet);
ed568912
KH
1040 iso_event &= ~(1 << i);
1041 }
1042
d60d7f1d
KH
1043 if (event & OHCI1394_cycle64Seconds) {
1044 cycle_time = reg_read(ohci, OHCI1394_IsochronousCycleTimer);
1045 if ((cycle_time & 0x80000000) == 0)
1046 ohci->bus_seconds++;
1047 }
1048
ed568912
KH
1049 return IRQ_HANDLED;
1050}
1051
2aef469a
KH
1052static int software_reset(struct fw_ohci *ohci)
1053{
1054 int i;
1055
1056 reg_write(ohci, OHCI1394_HCControlSet, OHCI1394_HCControl_softReset);
1057
1058 for (i = 0; i < OHCI_LOOP_COUNT; i++) {
1059 if ((reg_read(ohci, OHCI1394_HCControlSet) &
1060 OHCI1394_HCControl_softReset) == 0)
1061 return 0;
1062 msleep(1);
1063 }
1064
1065 return -EBUSY;
1066}
1067
ed568912
KH
1068static int ohci_enable(struct fw_card *card, u32 *config_rom, size_t length)
1069{
1070 struct fw_ohci *ohci = fw_ohci(card);
1071 struct pci_dev *dev = to_pci_dev(card->device);
1072
2aef469a
KH
1073 if (software_reset(ohci)) {
1074 fw_error("Failed to reset ohci card.\n");
1075 return -EBUSY;
1076 }
1077
1078 /*
1079 * Now enable LPS, which we need in order to start accessing
1080 * most of the registers. In fact, on some cards (ALI M5251),
1081 * accessing registers in the SClk domain without LPS enabled
1082 * will lock up the machine. Wait 50msec to make sure we have
1083 * full link enabled.
1084 */
1085 reg_write(ohci, OHCI1394_HCControlSet,
1086 OHCI1394_HCControl_LPS |
1087 OHCI1394_HCControl_postedWriteEnable);
1088 flush_writes(ohci);
1089 msleep(50);
1090
1091 reg_write(ohci, OHCI1394_HCControlClear,
1092 OHCI1394_HCControl_noByteSwapData);
1093
1094 reg_write(ohci, OHCI1394_LinkControlSet,
1095 OHCI1394_LinkControl_rcvSelfID |
1096 OHCI1394_LinkControl_cycleTimerEnable |
1097 OHCI1394_LinkControl_cycleMaster);
1098
1099 reg_write(ohci, OHCI1394_ATRetries,
1100 OHCI1394_MAX_AT_REQ_RETRIES |
1101 (OHCI1394_MAX_AT_RESP_RETRIES << 4) |
1102 (OHCI1394_MAX_PHYS_RESP_RETRIES << 8));
1103
1104 ar_context_run(&ohci->ar_request_ctx);
1105 ar_context_run(&ohci->ar_response_ctx);
1106
1107 reg_write(ohci, OHCI1394_SelfIDBuffer, ohci->self_id_bus);
1108 reg_write(ohci, OHCI1394_PhyUpperBound, 0x00010000);
1109 reg_write(ohci, OHCI1394_IntEventClear, ~0);
1110 reg_write(ohci, OHCI1394_IntMaskClear, ~0);
1111 reg_write(ohci, OHCI1394_IntMaskSet,
1112 OHCI1394_selfIDComplete |
1113 OHCI1394_RQPkt | OHCI1394_RSPkt |
1114 OHCI1394_reqTxComplete | OHCI1394_respTxComplete |
1115 OHCI1394_isochRx | OHCI1394_isochTx |
1116 OHCI1394_masterIntEnable |
1117 OHCI1394_cycle64Seconds);
1118
1119 /* Activate link_on bit and contender bit in our self ID packets.*/
1120 if (ohci_update_phy_reg(card, 4, 0,
1121 PHY_LINK_ACTIVE | PHY_CONTENDER) < 0)
1122 return -EIO;
1123
c781c06d
KH
1124 /*
1125 * When the link is not yet enabled, the atomic config rom
ed568912
KH
1126 * update mechanism described below in ohci_set_config_rom()
1127 * is not active. We have to update ConfigRomHeader and
1128 * BusOptions manually, and the write to ConfigROMmap takes
1129 * effect immediately. We tie this to the enabling of the
1130 * link, so we have a valid config rom before enabling - the
1131 * OHCI requires that ConfigROMhdr and BusOptions have valid
1132 * values before enabling.
1133 *
1134 * However, when the ConfigROMmap is written, some controllers
1135 * always read back quadlets 0 and 2 from the config rom to
1136 * the ConfigRomHeader and BusOptions registers on bus reset.
1137 * They shouldn't do that in this initial case where the link
1138 * isn't enabled. This means we have to use the same
1139 * workaround here, setting the bus header to 0 and then write
1140 * the right values in the bus reset tasklet.
1141 */
1142
1143 ohci->next_config_rom =
1144 dma_alloc_coherent(ohci->card.device, CONFIG_ROM_SIZE,
1145 &ohci->next_config_rom_bus, GFP_KERNEL);
1146 if (ohci->next_config_rom == NULL)
1147 return -ENOMEM;
1148
1149 memset(ohci->next_config_rom, 0, CONFIG_ROM_SIZE);
1150 fw_memcpy_to_be32(ohci->next_config_rom, config_rom, length * 4);
1151
1152 ohci->next_header = config_rom[0];
1153 ohci->next_config_rom[0] = 0;
1154 reg_write(ohci, OHCI1394_ConfigROMhdr, 0);
1155 reg_write(ohci, OHCI1394_BusOptions, config_rom[2]);
1156 reg_write(ohci, OHCI1394_ConfigROMmap, ohci->next_config_rom_bus);
1157
1158 reg_write(ohci, OHCI1394_AsReqFilterHiSet, 0x80000000);
1159
1160 if (request_irq(dev->irq, irq_handler,
65efffa8 1161 IRQF_SHARED, ohci_driver_name, ohci)) {
ed568912
KH
1162 fw_error("Failed to allocate shared interrupt %d.\n",
1163 dev->irq);
1164 dma_free_coherent(ohci->card.device, CONFIG_ROM_SIZE,
1165 ohci->config_rom, ohci->config_rom_bus);
1166 return -EIO;
1167 }
1168
1169 reg_write(ohci, OHCI1394_HCControlSet,
1170 OHCI1394_HCControl_linkEnable |
1171 OHCI1394_HCControl_BIBimageValid);
1172 flush_writes(ohci);
1173
c781c06d
KH
1174 /*
1175 * We are ready to go, initiate bus reset to finish the
1176 * initialization.
1177 */
ed568912
KH
1178
1179 fw_core_initiate_bus_reset(&ohci->card, 1);
1180
1181 return 0;
1182}
1183
1184static int
1185ohci_set_config_rom(struct fw_card *card, u32 *config_rom, size_t length)
1186{
1187 struct fw_ohci *ohci;
1188 unsigned long flags;
1189 int retval = 0;
1190 __be32 *next_config_rom;
1191 dma_addr_t next_config_rom_bus;
1192
1193 ohci = fw_ohci(card);
1194
c781c06d
KH
1195 /*
1196 * When the OHCI controller is enabled, the config rom update
ed568912
KH
1197 * mechanism is a bit tricky, but easy enough to use. See
1198 * section 5.5.6 in the OHCI specification.
1199 *
1200 * The OHCI controller caches the new config rom address in a
1201 * shadow register (ConfigROMmapNext) and needs a bus reset
1202 * for the changes to take place. When the bus reset is
1203 * detected, the controller loads the new values for the
1204 * ConfigRomHeader and BusOptions registers from the specified
1205 * config rom and loads ConfigROMmap from the ConfigROMmapNext
1206 * shadow register. All automatically and atomically.
1207 *
1208 * Now, there's a twist to this story. The automatic load of
1209 * ConfigRomHeader and BusOptions doesn't honor the
1210 * noByteSwapData bit, so with a be32 config rom, the
1211 * controller will load be32 values in to these registers
1212 * during the atomic update, even on litte endian
1213 * architectures. The workaround we use is to put a 0 in the
1214 * header quadlet; 0 is endian agnostic and means that the
1215 * config rom isn't ready yet. In the bus reset tasklet we
1216 * then set up the real values for the two registers.
1217 *
1218 * We use ohci->lock to avoid racing with the code that sets
1219 * ohci->next_config_rom to NULL (see bus_reset_tasklet).
1220 */
1221
1222 next_config_rom =
1223 dma_alloc_coherent(ohci->card.device, CONFIG_ROM_SIZE,
1224 &next_config_rom_bus, GFP_KERNEL);
1225 if (next_config_rom == NULL)
1226 return -ENOMEM;
1227
1228 spin_lock_irqsave(&ohci->lock, flags);
1229
1230 if (ohci->next_config_rom == NULL) {
1231 ohci->next_config_rom = next_config_rom;
1232 ohci->next_config_rom_bus = next_config_rom_bus;
1233
1234 memset(ohci->next_config_rom, 0, CONFIG_ROM_SIZE);
1235 fw_memcpy_to_be32(ohci->next_config_rom, config_rom,
1236 length * 4);
1237
1238 ohci->next_header = config_rom[0];
1239 ohci->next_config_rom[0] = 0;
1240
1241 reg_write(ohci, OHCI1394_ConfigROMmap,
1242 ohci->next_config_rom_bus);
1243 } else {
1244 dma_free_coherent(ohci->card.device, CONFIG_ROM_SIZE,
1245 next_config_rom, next_config_rom_bus);
1246 retval = -EBUSY;
1247 }
1248
1249 spin_unlock_irqrestore(&ohci->lock, flags);
1250
c781c06d
KH
1251 /*
1252 * Now initiate a bus reset to have the changes take
ed568912
KH
1253 * effect. We clean up the old config rom memory and DMA
1254 * mappings in the bus reset tasklet, since the OHCI
1255 * controller could need to access it before the bus reset
c781c06d
KH
1256 * takes effect.
1257 */
ed568912
KH
1258 if (retval == 0)
1259 fw_core_initiate_bus_reset(&ohci->card, 1);
1260
1261 return retval;
1262}
1263
1264static void ohci_send_request(struct fw_card *card, struct fw_packet *packet)
1265{
1266 struct fw_ohci *ohci = fw_ohci(card);
1267
1268 at_context_transmit(&ohci->at_request_ctx, packet);
1269}
1270
1271static void ohci_send_response(struct fw_card *card, struct fw_packet *packet)
1272{
1273 struct fw_ohci *ohci = fw_ohci(card);
1274
1275 at_context_transmit(&ohci->at_response_ctx, packet);
1276}
1277
730c32f5
KH
1278static int ohci_cancel_packet(struct fw_card *card, struct fw_packet *packet)
1279{
1280 struct fw_ohci *ohci = fw_ohci(card);
f319b6a0
KH
1281 struct context *ctx = &ohci->at_request_ctx;
1282 struct driver_data *driver_data = packet->driver_data;
1283 int retval = -ENOENT;
730c32f5 1284
f319b6a0 1285 tasklet_disable(&ctx->tasklet);
730c32f5 1286
f319b6a0
KH
1287 if (packet->ack != 0)
1288 goto out;
730c32f5 1289
f319b6a0
KH
1290 driver_data->packet = NULL;
1291 packet->ack = RCODE_CANCELLED;
1292 packet->callback(packet, &ohci->card, packet->ack);
1293 retval = 0;
730c32f5 1294
f319b6a0
KH
1295 out:
1296 tasklet_enable(&ctx->tasklet);
730c32f5 1297
f319b6a0 1298 return retval;
730c32f5
KH
1299}
1300
ed568912
KH
1301static int
1302ohci_enable_phys_dma(struct fw_card *card, int node_id, int generation)
1303{
1304 struct fw_ohci *ohci = fw_ohci(card);
1305 unsigned long flags;
907293d7 1306 int n, retval = 0;
ed568912 1307
c781c06d
KH
1308 /*
1309 * FIXME: Make sure this bitmask is cleared when we clear the busReset
1310 * interrupt bit. Clear physReqResourceAllBuses on bus reset.
1311 */
ed568912
KH
1312
1313 spin_lock_irqsave(&ohci->lock, flags);
1314
1315 if (ohci->generation != generation) {
1316 retval = -ESTALE;
1317 goto out;
1318 }
1319
c781c06d
KH
1320 /*
1321 * Note, if the node ID contains a non-local bus ID, physical DMA is
1322 * enabled for _all_ nodes on remote buses.
1323 */
907293d7
SR
1324
1325 n = (node_id & 0xffc0) == LOCAL_BUS ? node_id & 0x3f : 63;
1326 if (n < 32)
1327 reg_write(ohci, OHCI1394_PhyReqFilterLoSet, 1 << n);
1328 else
1329 reg_write(ohci, OHCI1394_PhyReqFilterHiSet, 1 << (n - 32));
1330
ed568912 1331 flush_writes(ohci);
ed568912 1332 out:
6cad95fe 1333 spin_unlock_irqrestore(&ohci->lock, flags);
ed568912
KH
1334 return retval;
1335}
373b2edd 1336
d60d7f1d
KH
1337static u64
1338ohci_get_bus_time(struct fw_card *card)
1339{
1340 struct fw_ohci *ohci = fw_ohci(card);
1341 u32 cycle_time;
1342 u64 bus_time;
1343
1344 cycle_time = reg_read(ohci, OHCI1394_IsochronousCycleTimer);
1345 bus_time = ((u64) ohci->bus_seconds << 32) | cycle_time;
1346
1347 return bus_time;
1348}
1349
d2746dc1
KH
1350static int handle_ir_dualbuffer_packet(struct context *context,
1351 struct descriptor *d,
1352 struct descriptor *last)
ed568912 1353{
295e3feb
KH
1354 struct iso_context *ctx =
1355 container_of(context, struct iso_context, context);
1356 struct db_descriptor *db = (struct db_descriptor *) d;
c70dc788 1357 __le32 *ir_header;
9b32d5f3 1358 size_t header_length;
c70dc788
KH
1359 void *p, *end;
1360 int i;
d2746dc1 1361
295e3feb
KH
1362 if (db->first_res_count > 0 && db->second_res_count > 0)
1363 /* This descriptor isn't done yet, stop iteration. */
1364 return 0;
1365
c70dc788
KH
1366 header_length = le16_to_cpu(db->first_req_count) -
1367 le16_to_cpu(db->first_res_count);
1368
1369 i = ctx->header_length;
1370 p = db + 1;
1371 end = p + header_length;
1372 while (p < end && i + ctx->base.header_size <= PAGE_SIZE) {
c781c06d
KH
1373 /*
1374 * The iso header is byteswapped to little endian by
15536221
KH
1375 * the controller, but the remaining header quadlets
1376 * are big endian. We want to present all the headers
1377 * as big endian, so we have to swap the first
c781c06d
KH
1378 * quadlet.
1379 */
15536221
KH
1380 *(u32 *) (ctx->header + i) = __swab32(*(u32 *) (p + 4));
1381 memcpy(ctx->header + i + 4, p + 8, ctx->base.header_size - 4);
c70dc788
KH
1382 i += ctx->base.header_size;
1383 p += ctx->base.header_size + 4;
1384 }
1385
1386 ctx->header_length = i;
9b32d5f3 1387
a77754a7 1388 if (le16_to_cpu(db->control) & DESCRIPTOR_IRQ_ALWAYS) {
c70dc788
KH
1389 ir_header = (__le32 *) (db + 1);
1390 ctx->base.callback(&ctx->base,
1391 le32_to_cpu(ir_header[0]) & 0xffff,
9b32d5f3 1392 ctx->header_length, ctx->header,
295e3feb 1393 ctx->base.callback_data);
9b32d5f3
KH
1394 ctx->header_length = 0;
1395 }
ed568912 1396
295e3feb 1397 return 1;
ed568912
KH
1398}
1399
30200739
KH
1400static int handle_it_packet(struct context *context,
1401 struct descriptor *d,
1402 struct descriptor *last)
ed568912 1403{
30200739
KH
1404 struct iso_context *ctx =
1405 container_of(context, struct iso_context, context);
373b2edd 1406
30200739
KH
1407 if (last->transfer_status == 0)
1408 /* This descriptor isn't done yet, stop iteration. */
1409 return 0;
1410
a77754a7 1411 if (le16_to_cpu(last->control) & DESCRIPTOR_IRQ_ALWAYS)
9b32d5f3
KH
1412 ctx->base.callback(&ctx->base, le16_to_cpu(last->res_count),
1413 0, NULL, ctx->base.callback_data);
30200739
KH
1414
1415 return 1;
ed568912
KH
1416}
1417
30200739 1418static struct fw_iso_context *
eb0306ea 1419ohci_allocate_iso_context(struct fw_card *card, int type, size_t header_size)
ed568912
KH
1420{
1421 struct fw_ohci *ohci = fw_ohci(card);
1422 struct iso_context *ctx, *list;
30200739 1423 descriptor_callback_t callback;
295e3feb 1424 u32 *mask, regs;
ed568912 1425 unsigned long flags;
9b32d5f3 1426 int index, retval = -ENOMEM;
ed568912
KH
1427
1428 if (type == FW_ISO_CONTEXT_TRANSMIT) {
1429 mask = &ohci->it_context_mask;
1430 list = ohci->it_context_list;
30200739 1431 callback = handle_it_packet;
ed568912 1432 } else {
373b2edd
SR
1433 mask = &ohci->ir_context_mask;
1434 list = ohci->ir_context_list;
c70dc788 1435 callback = handle_ir_dualbuffer_packet;
ed568912
KH
1436 }
1437
c70dc788 1438 /* FIXME: We need a fallback for pre 1.1 OHCI. */
e364cf4e
KH
1439 if (callback == handle_ir_dualbuffer_packet &&
1440 ohci->version < OHCI_VERSION_1_1)
1441 return ERR_PTR(-EINVAL);
1442
ed568912
KH
1443 spin_lock_irqsave(&ohci->lock, flags);
1444 index = ffs(*mask) - 1;
1445 if (index >= 0)
1446 *mask &= ~(1 << index);
1447 spin_unlock_irqrestore(&ohci->lock, flags);
1448
1449 if (index < 0)
1450 return ERR_PTR(-EBUSY);
1451
373b2edd
SR
1452 if (type == FW_ISO_CONTEXT_TRANSMIT)
1453 regs = OHCI1394_IsoXmitContextBase(index);
1454 else
1455 regs = OHCI1394_IsoRcvContextBase(index);
1456
ed568912 1457 ctx = &list[index];
2d826cc5 1458 memset(ctx, 0, sizeof(*ctx));
9b32d5f3
KH
1459 ctx->header_length = 0;
1460 ctx->header = (void *) __get_free_page(GFP_KERNEL);
1461 if (ctx->header == NULL)
1462 goto out;
1463
30200739 1464 retval = context_init(&ctx->context, ohci, ISO_BUFFER_SIZE,
295e3feb 1465 regs, callback);
9b32d5f3
KH
1466 if (retval < 0)
1467 goto out_with_header;
ed568912
KH
1468
1469 return &ctx->base;
9b32d5f3
KH
1470
1471 out_with_header:
1472 free_page((unsigned long)ctx->header);
1473 out:
1474 spin_lock_irqsave(&ohci->lock, flags);
1475 *mask |= 1 << index;
1476 spin_unlock_irqrestore(&ohci->lock, flags);
1477
1478 return ERR_PTR(retval);
ed568912
KH
1479}
1480
eb0306ea
KH
1481static int ohci_start_iso(struct fw_iso_context *base,
1482 s32 cycle, u32 sync, u32 tags)
ed568912 1483{
373b2edd 1484 struct iso_context *ctx = container_of(base, struct iso_context, base);
30200739 1485 struct fw_ohci *ohci = ctx->context.ohci;
8a2f7d93 1486 u32 control, match;
ed568912
KH
1487 int index;
1488
295e3feb
KH
1489 if (ctx->base.type == FW_ISO_CONTEXT_TRANSMIT) {
1490 index = ctx - ohci->it_context_list;
8a2f7d93
KH
1491 match = 0;
1492 if (cycle >= 0)
1493 match = IT_CONTEXT_CYCLE_MATCH_ENABLE |
295e3feb 1494 (cycle & 0x7fff) << 16;
21efb3cf 1495
295e3feb
KH
1496 reg_write(ohci, OHCI1394_IsoXmitIntEventClear, 1 << index);
1497 reg_write(ohci, OHCI1394_IsoXmitIntMaskSet, 1 << index);
8a2f7d93 1498 context_run(&ctx->context, match);
295e3feb
KH
1499 } else {
1500 index = ctx - ohci->ir_context_list;
8a2f7d93
KH
1501 control = IR_CONTEXT_DUAL_BUFFER_MODE | IR_CONTEXT_ISOCH_HEADER;
1502 match = (tags << 28) | (sync << 8) | ctx->base.channel;
1503 if (cycle >= 0) {
1504 match |= (cycle & 0x07fff) << 12;
1505 control |= IR_CONTEXT_CYCLE_MATCH_ENABLE;
1506 }
ed568912 1507
295e3feb
KH
1508 reg_write(ohci, OHCI1394_IsoRecvIntEventClear, 1 << index);
1509 reg_write(ohci, OHCI1394_IsoRecvIntMaskSet, 1 << index);
a77754a7 1510 reg_write(ohci, CONTEXT_MATCH(ctx->context.regs), match);
8a2f7d93 1511 context_run(&ctx->context, control);
295e3feb 1512 }
ed568912
KH
1513
1514 return 0;
1515}
1516
b8295668
KH
1517static int ohci_stop_iso(struct fw_iso_context *base)
1518{
1519 struct fw_ohci *ohci = fw_ohci(base->card);
373b2edd 1520 struct iso_context *ctx = container_of(base, struct iso_context, base);
b8295668
KH
1521 int index;
1522
1523 if (ctx->base.type == FW_ISO_CONTEXT_TRANSMIT) {
1524 index = ctx - ohci->it_context_list;
1525 reg_write(ohci, OHCI1394_IsoXmitIntMaskClear, 1 << index);
1526 } else {
1527 index = ctx - ohci->ir_context_list;
1528 reg_write(ohci, OHCI1394_IsoRecvIntMaskClear, 1 << index);
1529 }
1530 flush_writes(ohci);
1531 context_stop(&ctx->context);
1532
1533 return 0;
1534}
1535
ed568912
KH
1536static void ohci_free_iso_context(struct fw_iso_context *base)
1537{
1538 struct fw_ohci *ohci = fw_ohci(base->card);
373b2edd 1539 struct iso_context *ctx = container_of(base, struct iso_context, base);
ed568912
KH
1540 unsigned long flags;
1541 int index;
1542
b8295668
KH
1543 ohci_stop_iso(base);
1544 context_release(&ctx->context);
9b32d5f3 1545 free_page((unsigned long)ctx->header);
b8295668 1546
ed568912
KH
1547 spin_lock_irqsave(&ohci->lock, flags);
1548
1549 if (ctx->base.type == FW_ISO_CONTEXT_TRANSMIT) {
1550 index = ctx - ohci->it_context_list;
ed568912
KH
1551 ohci->it_context_mask |= 1 << index;
1552 } else {
1553 index = ctx - ohci->ir_context_list;
ed568912
KH
1554 ohci->ir_context_mask |= 1 << index;
1555 }
ed568912
KH
1556
1557 spin_unlock_irqrestore(&ohci->lock, flags);
1558}
1559
1560static int
295e3feb
KH
1561ohci_queue_iso_transmit(struct fw_iso_context *base,
1562 struct fw_iso_packet *packet,
1563 struct fw_iso_buffer *buffer,
1564 unsigned long payload)
ed568912 1565{
373b2edd 1566 struct iso_context *ctx = container_of(base, struct iso_context, base);
30200739 1567 struct descriptor *d, *last, *pd;
ed568912
KH
1568 struct fw_iso_packet *p;
1569 __le32 *header;
9aad8125 1570 dma_addr_t d_bus, page_bus;
ed568912
KH
1571 u32 z, header_z, payload_z, irq;
1572 u32 payload_index, payload_end_index, next_page_index;
30200739 1573 int page, end_page, i, length, offset;
ed568912 1574
c781c06d
KH
1575 /*
1576 * FIXME: Cycle lost behavior should be configurable: lose
1577 * packet, retransmit or terminate..
1578 */
ed568912
KH
1579
1580 p = packet;
9aad8125 1581 payload_index = payload;
ed568912
KH
1582
1583 if (p->skip)
1584 z = 1;
1585 else
1586 z = 2;
1587 if (p->header_length > 0)
1588 z++;
1589
1590 /* Determine the first page the payload isn't contained in. */
1591 end_page = PAGE_ALIGN(payload_index + p->payload_length) >> PAGE_SHIFT;
1592 if (p->payload_length > 0)
1593 payload_z = end_page - (payload_index >> PAGE_SHIFT);
1594 else
1595 payload_z = 0;
1596
1597 z += payload_z;
1598
1599 /* Get header size in number of descriptors. */
2d826cc5 1600 header_z = DIV_ROUND_UP(p->header_length, sizeof(*d));
ed568912 1601
30200739
KH
1602 d = context_get_descriptors(&ctx->context, z + header_z, &d_bus);
1603 if (d == NULL)
1604 return -ENOMEM;
ed568912
KH
1605
1606 if (!p->skip) {
a77754a7 1607 d[0].control = cpu_to_le16(DESCRIPTOR_KEY_IMMEDIATE);
ed568912
KH
1608 d[0].req_count = cpu_to_le16(8);
1609
1610 header = (__le32 *) &d[1];
a77754a7
KH
1611 header[0] = cpu_to_le32(IT_HEADER_SY(p->sy) |
1612 IT_HEADER_TAG(p->tag) |
1613 IT_HEADER_TCODE(TCODE_STREAM_DATA) |
1614 IT_HEADER_CHANNEL(ctx->base.channel) |
1615 IT_HEADER_SPEED(ctx->base.speed));
ed568912 1616 header[1] =
a77754a7 1617 cpu_to_le32(IT_HEADER_DATA_LENGTH(p->header_length +
ed568912
KH
1618 p->payload_length));
1619 }
1620
1621 if (p->header_length > 0) {
1622 d[2].req_count = cpu_to_le16(p->header_length);
2d826cc5 1623 d[2].data_address = cpu_to_le32(d_bus + z * sizeof(*d));
ed568912
KH
1624 memcpy(&d[z], p->header, p->header_length);
1625 }
1626
1627 pd = d + z - payload_z;
1628 payload_end_index = payload_index + p->payload_length;
1629 for (i = 0; i < payload_z; i++) {
1630 page = payload_index >> PAGE_SHIFT;
1631 offset = payload_index & ~PAGE_MASK;
1632 next_page_index = (page + 1) << PAGE_SHIFT;
1633 length =
1634 min(next_page_index, payload_end_index) - payload_index;
1635 pd[i].req_count = cpu_to_le16(length);
9aad8125
KH
1636
1637 page_bus = page_private(buffer->pages[page]);
1638 pd[i].data_address = cpu_to_le32(page_bus + offset);
ed568912
KH
1639
1640 payload_index += length;
1641 }
1642
ed568912 1643 if (p->interrupt)
a77754a7 1644 irq = DESCRIPTOR_IRQ_ALWAYS;
ed568912 1645 else
a77754a7 1646 irq = DESCRIPTOR_NO_IRQ;
ed568912 1647
30200739 1648 last = z == 2 ? d : d + z - 1;
a77754a7
KH
1649 last->control |= cpu_to_le16(DESCRIPTOR_OUTPUT_LAST |
1650 DESCRIPTOR_STATUS |
1651 DESCRIPTOR_BRANCH_ALWAYS |
cbb59da7 1652 irq);
ed568912 1653
30200739 1654 context_append(&ctx->context, d, z, header_z);
ed568912
KH
1655
1656 return 0;
1657}
373b2edd 1658
295e3feb 1659static int
d2746dc1
KH
1660ohci_queue_iso_receive_dualbuffer(struct fw_iso_context *base,
1661 struct fw_iso_packet *packet,
1662 struct fw_iso_buffer *buffer,
1663 unsigned long payload)
295e3feb
KH
1664{
1665 struct iso_context *ctx = container_of(base, struct iso_context, base);
1666 struct db_descriptor *db = NULL;
1667 struct descriptor *d;
1668 struct fw_iso_packet *p;
1669 dma_addr_t d_bus, page_bus;
1670 u32 z, header_z, length, rest;
c70dc788 1671 int page, offset, packet_count, header_size;
373b2edd 1672
c781c06d
KH
1673 /*
1674 * FIXME: Cycle lost behavior should be configurable: lose
1675 * packet, retransmit or terminate..
1676 */
295e3feb 1677
c70dc788
KH
1678 if (packet->skip) {
1679 d = context_get_descriptors(&ctx->context, 2, &d_bus);
1680 if (d == NULL)
1681 return -ENOMEM;
1682
1683 db = (struct db_descriptor *) d;
a77754a7
KH
1684 db->control = cpu_to_le16(DESCRIPTOR_STATUS |
1685 DESCRIPTOR_BRANCH_ALWAYS |
1686 DESCRIPTOR_WAIT);
c70dc788
KH
1687 db->first_size = cpu_to_le16(ctx->base.header_size + 4);
1688 context_append(&ctx->context, d, 2, 0);
1689 }
98b6cbe8 1690
295e3feb
KH
1691 p = packet;
1692 z = 2;
1693
c781c06d
KH
1694 /*
1695 * The OHCI controller puts the status word in the header
1696 * buffer too, so we need 4 extra bytes per packet.
1697 */
c70dc788
KH
1698 packet_count = p->header_length / ctx->base.header_size;
1699 header_size = packet_count * (ctx->base.header_size + 4);
1700
295e3feb 1701 /* Get header size in number of descriptors. */
2d826cc5 1702 header_z = DIV_ROUND_UP(header_size, sizeof(*d));
295e3feb
KH
1703 page = payload >> PAGE_SHIFT;
1704 offset = payload & ~PAGE_MASK;
1705 rest = p->payload_length;
1706
1707 /* FIXME: OHCI 1.0 doesn't support dual buffer receive */
295e3feb
KH
1708 /* FIXME: make packet-per-buffer/dual-buffer a context option */
1709 while (rest > 0) {
1710 d = context_get_descriptors(&ctx->context,
1711 z + header_z, &d_bus);
1712 if (d == NULL)
1713 return -ENOMEM;
1714
1715 db = (struct db_descriptor *) d;
a77754a7
KH
1716 db->control = cpu_to_le16(DESCRIPTOR_STATUS |
1717 DESCRIPTOR_BRANCH_ALWAYS);
c70dc788
KH
1718 db->first_size = cpu_to_le16(ctx->base.header_size + 4);
1719 db->first_req_count = cpu_to_le16(header_size);
1e1d196b 1720 db->first_res_count = db->first_req_count;
2d826cc5 1721 db->first_buffer = cpu_to_le32(d_bus + sizeof(*db));
373b2edd 1722
295e3feb
KH
1723 if (offset + rest < PAGE_SIZE)
1724 length = rest;
1725 else
1726 length = PAGE_SIZE - offset;
1727
1e1d196b
KH
1728 db->second_req_count = cpu_to_le16(length);
1729 db->second_res_count = db->second_req_count;
295e3feb
KH
1730 page_bus = page_private(buffer->pages[page]);
1731 db->second_buffer = cpu_to_le32(page_bus + offset);
1732
cb2d2cdb 1733 if (p->interrupt && length == rest)
a77754a7 1734 db->control |= cpu_to_le16(DESCRIPTOR_IRQ_ALWAYS);
cb2d2cdb 1735
295e3feb
KH
1736 context_append(&ctx->context, d, z, header_z);
1737 offset = (offset + length) & ~PAGE_MASK;
1738 rest -= length;
1739 page++;
1740 }
1741
d2746dc1
KH
1742 return 0;
1743}
21efb3cf 1744
295e3feb
KH
1745static int
1746ohci_queue_iso(struct fw_iso_context *base,
1747 struct fw_iso_packet *packet,
1748 struct fw_iso_buffer *buffer,
1749 unsigned long payload)
1750{
e364cf4e
KH
1751 struct iso_context *ctx = container_of(base, struct iso_context, base);
1752
295e3feb
KH
1753 if (base->type == FW_ISO_CONTEXT_TRANSMIT)
1754 return ohci_queue_iso_transmit(base, packet, buffer, payload);
e364cf4e 1755 else if (ctx->context.ohci->version >= OHCI_VERSION_1_1)
d2746dc1
KH
1756 return ohci_queue_iso_receive_dualbuffer(base, packet,
1757 buffer, payload);
e364cf4e
KH
1758 else
1759 /* FIXME: Implement fallback for OHCI 1.0 controllers. */
1760 return -EINVAL;
295e3feb
KH
1761}
1762
21ebcd12 1763static const struct fw_card_driver ohci_driver = {
ed568912
KH
1764 .name = ohci_driver_name,
1765 .enable = ohci_enable,
1766 .update_phy_reg = ohci_update_phy_reg,
1767 .set_config_rom = ohci_set_config_rom,
1768 .send_request = ohci_send_request,
1769 .send_response = ohci_send_response,
730c32f5 1770 .cancel_packet = ohci_cancel_packet,
ed568912 1771 .enable_phys_dma = ohci_enable_phys_dma,
d60d7f1d 1772 .get_bus_time = ohci_get_bus_time,
ed568912
KH
1773
1774 .allocate_iso_context = ohci_allocate_iso_context,
1775 .free_iso_context = ohci_free_iso_context,
1776 .queue_iso = ohci_queue_iso,
69cdb726 1777 .start_iso = ohci_start_iso,
b8295668 1778 .stop_iso = ohci_stop_iso,
ed568912
KH
1779};
1780
ed568912
KH
1781static int __devinit
1782pci_probe(struct pci_dev *dev, const struct pci_device_id *ent)
1783{
1784 struct fw_ohci *ohci;
e364cf4e 1785 u32 bus_options, max_receive, link_speed;
ed568912 1786 u64 guid;
d79406dd 1787 int err;
ed568912
KH
1788 size_t size;
1789
2d826cc5 1790 ohci = kzalloc(sizeof(*ohci), GFP_KERNEL);
ed568912
KH
1791 if (ohci == NULL) {
1792 fw_error("Could not malloc fw_ohci data.\n");
1793 return -ENOMEM;
1794 }
1795
1796 fw_card_initialize(&ohci->card, &ohci_driver, &dev->dev);
1797
d79406dd
KH
1798 err = pci_enable_device(dev);
1799 if (err) {
ed568912 1800 fw_error("Failed to enable OHCI hardware.\n");
d79406dd 1801 goto fail_put_card;
ed568912
KH
1802 }
1803
1804 pci_set_master(dev);
1805 pci_write_config_dword(dev, OHCI1394_PCI_HCI_Control, 0);
1806 pci_set_drvdata(dev, ohci);
1807
1808 spin_lock_init(&ohci->lock);
1809
1810 tasklet_init(&ohci->bus_reset_tasklet,
1811 bus_reset_tasklet, (unsigned long)ohci);
1812
d79406dd
KH
1813 err = pci_request_region(dev, 0, ohci_driver_name);
1814 if (err) {
ed568912 1815 fw_error("MMIO resource unavailable\n");
d79406dd 1816 goto fail_disable;
ed568912
KH
1817 }
1818
1819 ohci->registers = pci_iomap(dev, 0, OHCI1394_REGISTER_SIZE);
1820 if (ohci->registers == NULL) {
1821 fw_error("Failed to remap registers\n");
d79406dd
KH
1822 err = -ENXIO;
1823 goto fail_iomem;
ed568912
KH
1824 }
1825
ed568912
KH
1826 ar_context_init(&ohci->ar_request_ctx, ohci,
1827 OHCI1394_AsReqRcvContextControlSet);
1828
1829 ar_context_init(&ohci->ar_response_ctx, ohci,
1830 OHCI1394_AsRspRcvContextControlSet);
1831
f319b6a0
KH
1832 context_init(&ohci->at_request_ctx, ohci, AT_BUFFER_SIZE,
1833 OHCI1394_AsReqTrContextControlSet, handle_at_packet);
ed568912 1834
f319b6a0
KH
1835 context_init(&ohci->at_response_ctx, ohci, AT_BUFFER_SIZE,
1836 OHCI1394_AsRspTrContextControlSet, handle_at_packet);
ed568912 1837
ed568912
KH
1838 reg_write(ohci, OHCI1394_IsoRecvIntMaskSet, ~0);
1839 ohci->it_context_mask = reg_read(ohci, OHCI1394_IsoRecvIntMaskSet);
1840 reg_write(ohci, OHCI1394_IsoRecvIntMaskClear, ~0);
1841 size = sizeof(struct iso_context) * hweight32(ohci->it_context_mask);
1842 ohci->it_context_list = kzalloc(size, GFP_KERNEL);
1843
1844 reg_write(ohci, OHCI1394_IsoXmitIntMaskSet, ~0);
1845 ohci->ir_context_mask = reg_read(ohci, OHCI1394_IsoXmitIntMaskSet);
1846 reg_write(ohci, OHCI1394_IsoXmitIntMaskClear, ~0);
1847 size = sizeof(struct iso_context) * hweight32(ohci->ir_context_mask);
1848 ohci->ir_context_list = kzalloc(size, GFP_KERNEL);
1849
1850 if (ohci->it_context_list == NULL || ohci->ir_context_list == NULL) {
1851 fw_error("Out of memory for it/ir contexts.\n");
d79406dd
KH
1852 err = -ENOMEM;
1853 goto fail_registers;
ed568912
KH
1854 }
1855
1856 /* self-id dma buffer allocation */
1857 ohci->self_id_cpu = dma_alloc_coherent(ohci->card.device,
1858 SELF_ID_BUF_SIZE,
1859 &ohci->self_id_bus,
1860 GFP_KERNEL);
1861 if (ohci->self_id_cpu == NULL) {
1862 fw_error("Out of memory for self ID buffer.\n");
d79406dd
KH
1863 err = -ENOMEM;
1864 goto fail_registers;
ed568912
KH
1865 }
1866
ed568912
KH
1867 bus_options = reg_read(ohci, OHCI1394_BusOptions);
1868 max_receive = (bus_options >> 12) & 0xf;
1869 link_speed = bus_options & 0x7;
1870 guid = ((u64) reg_read(ohci, OHCI1394_GUIDHi) << 32) |
1871 reg_read(ohci, OHCI1394_GUIDLo);
1872
d79406dd
KH
1873 err = fw_card_add(&ohci->card, max_receive, link_speed, guid);
1874 if (err < 0)
1875 goto fail_self_id;
ed568912 1876
e364cf4e 1877 ohci->version = reg_read(ohci, OHCI1394_Version) & 0x00ff00ff;
500be725 1878 fw_notify("Added fw-ohci device %s, OHCI version %x.%x\n",
e364cf4e 1879 dev->dev.bus_id, ohci->version >> 16, ohci->version & 0xff);
ed568912
KH
1880
1881 return 0;
d79406dd
KH
1882
1883 fail_self_id:
1884 dma_free_coherent(ohci->card.device, SELF_ID_BUF_SIZE,
1885 ohci->self_id_cpu, ohci->self_id_bus);
1886 fail_registers:
1887 kfree(ohci->it_context_list);
1888 kfree(ohci->ir_context_list);
1889 pci_iounmap(dev, ohci->registers);
1890 fail_iomem:
1891 pci_release_region(dev, 0);
1892 fail_disable:
1893 pci_disable_device(dev);
1894 fail_put_card:
1895 fw_card_put(&ohci->card);
1896
1897 return err;
ed568912
KH
1898}
1899
1900static void pci_remove(struct pci_dev *dev)
1901{
1902 struct fw_ohci *ohci;
1903
1904 ohci = pci_get_drvdata(dev);
e254a4b4
KH
1905 reg_write(ohci, OHCI1394_IntMaskClear, ~0);
1906 flush_writes(ohci);
ed568912
KH
1907 fw_core_remove_card(&ohci->card);
1908
c781c06d
KH
1909 /*
1910 * FIXME: Fail all pending packets here, now that the upper
1911 * layers can't queue any more.
1912 */
ed568912
KH
1913
1914 software_reset(ohci);
1915 free_irq(dev->irq, ohci);
d79406dd
KH
1916 dma_free_coherent(ohci->card.device, SELF_ID_BUF_SIZE,
1917 ohci->self_id_cpu, ohci->self_id_bus);
1918 kfree(ohci->it_context_list);
1919 kfree(ohci->ir_context_list);
1920 pci_iounmap(dev, ohci->registers);
1921 pci_release_region(dev, 0);
1922 pci_disable_device(dev);
1923 fw_card_put(&ohci->card);
ed568912
KH
1924
1925 fw_notify("Removed fw-ohci device.\n");
1926}
1927
2aef469a
KH
1928#ifdef CONFIG_PM
1929static int pci_suspend(struct pci_dev *pdev, pm_message_t state)
1930{
1931 struct fw_ohci *ohci = pci_get_drvdata(pdev);
1932 int err;
1933
1934 software_reset(ohci);
1935 free_irq(pdev->irq, ohci);
1936 err = pci_save_state(pdev);
1937 if (err) {
8a8cea27 1938 fw_error("pci_save_state failed\n");
2aef469a
KH
1939 return err;
1940 }
1941 err = pci_set_power_state(pdev, pci_choose_state(pdev, state));
1942 if (err) {
8a8cea27 1943 fw_error("pci_set_power_state failed\n");
2aef469a
KH
1944 return err;
1945 }
1946
1947 return 0;
1948}
1949
1950static int pci_resume(struct pci_dev *pdev)
1951{
1952 struct fw_ohci *ohci = pci_get_drvdata(pdev);
1953 int err;
1954
1955 pci_set_power_state(pdev, PCI_D0);
1956 pci_restore_state(pdev);
1957 err = pci_enable_device(pdev);
1958 if (err) {
8a8cea27 1959 fw_error("pci_enable_device failed\n");
2aef469a
KH
1960 return err;
1961 }
1962
1963 return ohci_enable(&ohci->card, ohci->config_rom, CONFIG_ROM_SIZE);
1964}
1965#endif
1966
ed568912
KH
1967static struct pci_device_id pci_table[] = {
1968 { PCI_DEVICE_CLASS(PCI_CLASS_SERIAL_FIREWIRE_OHCI, ~0) },
1969 { }
1970};
1971
1972MODULE_DEVICE_TABLE(pci, pci_table);
1973
1974static struct pci_driver fw_ohci_pci_driver = {
1975 .name = ohci_driver_name,
1976 .id_table = pci_table,
1977 .probe = pci_probe,
1978 .remove = pci_remove,
2aef469a
KH
1979#ifdef CONFIG_PM
1980 .resume = pci_resume,
1981 .suspend = pci_suspend,
1982#endif
ed568912
KH
1983};
1984
1985MODULE_AUTHOR("Kristian Hoegsberg <krh@bitplanet.net>");
1986MODULE_DESCRIPTION("Driver for PCI OHCI IEEE1394 controllers");
1987MODULE_LICENSE("GPL");
1988
1e4c7b0d
OH
1989/* Provide a module alias so root-on-sbp2 initrds don't break. */
1990#ifndef CONFIG_IEEE1394_OHCI1394_MODULE
1991MODULE_ALIAS("ohci1394");
1992#endif
1993
ed568912
KH
1994static int __init fw_ohci_init(void)
1995{
1996 return pci_register_driver(&fw_ohci_pci_driver);
1997}
1998
1999static void __exit fw_ohci_cleanup(void)
2000{
2001 pci_unregister_driver(&fw_ohci_pci_driver);
2002}
2003
2004module_init(fw_ohci_init);
2005module_exit(fw_ohci_cleanup);