blob: d296d12909d6f4d8c980c631a2a94502f2fbc213 [file] [log] [blame]
Kristian Høgsbergc781c062007-05-07 20:33:32 -04001/*
2 * Driver for OHCI 1394 controllers
Kristian Høgsberged568912006-12-19 19:58:35 -05003 *
Kristian Høgsberged568912006-12-19 19:58:35 -05004 * 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
Stefan Richtere524f6162007-08-20 21:58:30 +020021#include <linux/compiler.h>
Kristian Høgsberged568912006-12-19 19:58:35 -050022#include <linux/delay.h>
Stefan Richtere8ca9702009-06-04 21:09:38 +020023#include <linux/device.h>
Andrew Mortoncf3e72f2006-12-27 14:36:37 -080024#include <linux/dma-mapping.h>
Stefan Richtere8ca9702009-06-04 21:09:38 +020025#include <linux/firewire-constants.h>
Stefan Richterc26f0232007-08-20 21:40:30 +020026#include <linux/gfp.h>
Stefan Richtera7fb60d2007-08-20 21:41:22 +020027#include <linux/init.h>
28#include <linux/interrupt.h>
Stefan Richtere8ca9702009-06-04 21:09:38 +020029#include <linux/io.h>
Stefan Richtera7fb60d2007-08-20 21:41:22 +020030#include <linux/kernel.h>
Stefan Richtere8ca9702009-06-04 21:09:38 +020031#include <linux/list.h>
Al Virofaa2fb42007-05-15 20:36:10 +010032#include <linux/mm.h>
Stefan Richtera7fb60d2007-08-20 21:41:22 +020033#include <linux/module.h>
Stefan Richterad3c0fe2008-03-20 22:04:36 +010034#include <linux/moduleparam.h>
Stefan Richtera7fb60d2007-08-20 21:41:22 +020035#include <linux/pci.h>
Stefan Richterc26f0232007-08-20 21:40:30 +020036#include <linux/spinlock.h>
Stefan Richtere8ca9702009-06-04 21:09:38 +020037#include <linux/string.h>
Andrew Mortoncf3e72f2006-12-27 14:36:37 -080038
Stefan Richter3dcdc502009-06-04 21:08:43 +020039#include <asm/atomic.h>
Stefan Richtere8ca9702009-06-04 21:09:38 +020040#include <asm/byteorder.h>
Stefan Richterc26f0232007-08-20 21:40:30 +020041#include <asm/page.h>
Stefan Richteree71c2f2007-08-25 14:08:19 +020042#include <asm/system.h>
Kristian Høgsberged568912006-12-19 19:58:35 -050043
Stefan Richterea8d0062008-03-01 02:42:56 +010044#ifdef CONFIG_PPC_PMAC
45#include <asm/pmac_feature.h>
46#endif
47
Kristian Høgsberged568912006-12-19 19:58:35 -050048#include "fw-ohci.h"
Stefan Richtera7fb60d2007-08-20 21:41:22 +020049#include "fw-transaction.h"
Kristian Høgsberged568912006-12-19 19:58:35 -050050
Kristian Høgsberga77754a2007-05-07 20:33:35 -040051#define DESCRIPTOR_OUTPUT_MORE 0
52#define DESCRIPTOR_OUTPUT_LAST (1 << 12)
53#define DESCRIPTOR_INPUT_MORE (2 << 12)
54#define DESCRIPTOR_INPUT_LAST (3 << 12)
55#define DESCRIPTOR_STATUS (1 << 11)
56#define DESCRIPTOR_KEY_IMMEDIATE (2 << 8)
57#define DESCRIPTOR_PING (1 << 7)
58#define DESCRIPTOR_YY (1 << 6)
59#define DESCRIPTOR_NO_IRQ (0 << 4)
60#define DESCRIPTOR_IRQ_ERROR (1 << 4)
61#define DESCRIPTOR_IRQ_ALWAYS (3 << 4)
62#define DESCRIPTOR_BRANCH_ALWAYS (3 << 2)
63#define DESCRIPTOR_WAIT (3 << 0)
Kristian Høgsberged568912006-12-19 19:58:35 -050064
65struct descriptor {
66 __le16 req_count;
67 __le16 control;
68 __le32 data_address;
69 __le32 branch_address;
70 __le16 res_count;
71 __le16 transfer_status;
72} __attribute__((aligned(16)));
73
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -050074struct db_descriptor {
75 __le16 first_size;
76 __le16 control;
77 __le16 second_req_count;
78 __le16 first_req_count;
79 __le32 branch_address;
80 __le16 second_res_count;
81 __le16 first_res_count;
82 __le32 reserved0;
83 __le32 first_buffer;
84 __le32 second_buffer;
85 __le32 reserved1;
86} __attribute__((aligned(16)));
87
Kristian Høgsberga77754a2007-05-07 20:33:35 -040088#define CONTROL_SET(regs) (regs)
89#define CONTROL_CLEAR(regs) ((regs) + 4)
90#define COMMAND_PTR(regs) ((regs) + 12)
91#define CONTEXT_MATCH(regs) ((regs) + 16)
Kristian Høgsberg72e318e2007-02-06 14:49:31 -050092
Kristian Høgsberg32b46092007-02-06 14:49:30 -050093struct ar_buffer {
94 struct descriptor descriptor;
95 struct ar_buffer *next;
96 __le32 data[0];
97};
98
Kristian Høgsberged568912006-12-19 19:58:35 -050099struct ar_context {
100 struct fw_ohci *ohci;
Kristian Høgsberg32b46092007-02-06 14:49:30 -0500101 struct ar_buffer *current_buffer;
102 struct ar_buffer *last_buffer;
103 void *pointer;
Kristian Høgsberg72e318e2007-02-06 14:49:31 -0500104 u32 regs;
Kristian Høgsberged568912006-12-19 19:58:35 -0500105 struct tasklet_struct tasklet;
106};
107
Kristian Høgsberg30200732007-02-16 17:34:39 -0500108struct context;
109
110typedef int (*descriptor_callback_t)(struct context *ctx,
111 struct descriptor *d,
112 struct descriptor *last);
David Moorefe5ca632008-01-06 17:21:41 -0500113
114/*
115 * A buffer that contains a block of DMA-able coherent memory used for
116 * storing a portion of a DMA descriptor program.
117 */
118struct descriptor_buffer {
119 struct list_head list;
120 dma_addr_t buffer_bus;
121 size_t buffer_size;
122 size_t used;
123 struct descriptor buffer[0];
124};
125
Kristian Høgsberg30200732007-02-16 17:34:39 -0500126struct context {
Stefan Richter373b2ed2007-03-04 14:45:18 +0100127 struct fw_ohci *ohci;
Kristian Høgsberg30200732007-02-16 17:34:39 -0500128 u32 regs;
David Moorefe5ca632008-01-06 17:21:41 -0500129 int total_allocation;
Stefan Richter373b2ed2007-03-04 14:45:18 +0100130
David Moorefe5ca632008-01-06 17:21:41 -0500131 /*
132 * List of page-sized buffers for storing DMA descriptors.
133 * Head of list contains buffers in use and tail of list contains
134 * free buffers.
135 */
136 struct list_head buffer_list;
137
138 /*
139 * Pointer to a buffer inside buffer_list that contains the tail
140 * end of the current DMA program.
141 */
142 struct descriptor_buffer *buffer_tail;
143
144 /*
145 * The descriptor containing the branch address of the first
146 * descriptor that has not yet been filled by the device.
147 */
148 struct descriptor *last;
149
150 /*
151 * The last descriptor in the DMA program. It contains the branch
152 * address that must be updated upon appending a new descriptor.
153 */
154 struct descriptor *prev;
Kristian Høgsberg30200732007-02-16 17:34:39 -0500155
156 descriptor_callback_t callback;
157
Stefan Richter373b2ed2007-03-04 14:45:18 +0100158 struct tasklet_struct tasklet;
Kristian Høgsberg30200732007-02-16 17:34:39 -0500159};
Kristian Høgsberg30200732007-02-16 17:34:39 -0500160
Kristian Høgsberga77754a2007-05-07 20:33:35 -0400161#define IT_HEADER_SY(v) ((v) << 0)
162#define IT_HEADER_TCODE(v) ((v) << 4)
163#define IT_HEADER_CHANNEL(v) ((v) << 8)
164#define IT_HEADER_TAG(v) ((v) << 14)
165#define IT_HEADER_SPEED(v) ((v) << 16)
166#define IT_HEADER_DATA_LENGTH(v) ((v) << 16)
Kristian Høgsberged568912006-12-19 19:58:35 -0500167
168struct iso_context {
169 struct fw_iso_context base;
Kristian Høgsberg30200732007-02-16 17:34:39 -0500170 struct context context;
David Moore0642b652007-12-19 03:09:18 -0500171 int excess_bytes;
Kristian Høgsberg9b32d5f2007-02-16 17:34:44 -0500172 void *header;
173 size_t header_length;
Kristian Høgsberged568912006-12-19 19:58:35 -0500174};
175
176#define CONFIG_ROM_SIZE 1024
177
178struct fw_ohci {
179 struct fw_card card;
180
181 __iomem char *registers;
182 dma_addr_t self_id_bus;
183 __le32 *self_id_cpu;
184 struct tasklet_struct bus_reset_tasklet;
Kristian Høgsberge636fe22007-01-26 00:38:04 -0500185 int node_id;
Kristian Høgsberged568912006-12-19 19:58:35 -0500186 int generation;
Stefan Richtere09770d2008-03-11 02:23:29 +0100187 int request_generation; /* for timestamping incoming requests */
Stefan Richter3dcdc502009-06-04 21:08:43 +0200188 atomic_t bus_seconds;
Stefan Richter95984f62008-07-22 18:41:10 +0200189
190 bool use_dualbuffer;
Stefan Richter11bf20a2008-03-01 02:47:15 +0100191 bool old_uninorth;
Stefan Richterd34316a2008-04-12 22:31:25 +0200192 bool bus_reset_packet_quirk;
Kristian Høgsberged568912006-12-19 19:58:35 -0500193
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400194 /*
195 * Spinlock for accessing fw_ohci data. Never call out of
196 * this driver with this lock held.
197 */
Kristian Høgsberged568912006-12-19 19:58:35 -0500198 spinlock_t lock;
199 u32 self_id_buffer[512];
200
201 /* Config rom buffers */
202 __be32 *config_rom;
203 dma_addr_t config_rom_bus;
204 __be32 *next_config_rom;
205 dma_addr_t next_config_rom_bus;
206 u32 next_header;
207
208 struct ar_context ar_request_ctx;
209 struct ar_context ar_response_ctx;
Kristian Høgsbergf319b6a2007-03-07 12:12:49 -0500210 struct context at_request_ctx;
211 struct context at_response_ctx;
Kristian Høgsberged568912006-12-19 19:58:35 -0500212
213 u32 it_context_mask;
214 struct iso_context *it_context_list;
Stefan Richter4817ed22008-12-21 16:39:46 +0100215 u64 ir_context_channels;
Kristian Høgsberged568912006-12-19 19:58:35 -0500216 u32 ir_context_mask;
217 struct iso_context *ir_context_list;
218};
219
Adrian Bunk95688e92007-01-22 19:17:37 +0100220static inline struct fw_ohci *fw_ohci(struct fw_card *card)
Kristian Høgsberged568912006-12-19 19:58:35 -0500221{
222 return container_of(card, struct fw_ohci, card);
223}
224
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -0500225#define IT_CONTEXT_CYCLE_MATCH_ENABLE 0x80000000
226#define IR_CONTEXT_BUFFER_FILL 0x80000000
227#define IR_CONTEXT_ISOCH_HEADER 0x40000000
228#define IR_CONTEXT_CYCLE_MATCH_ENABLE 0x20000000
229#define IR_CONTEXT_MULTI_CHANNEL_MODE 0x10000000
230#define IR_CONTEXT_DUAL_BUFFER_MODE 0x08000000
Kristian Høgsberged568912006-12-19 19:58:35 -0500231
232#define CONTEXT_RUN 0x8000
233#define CONTEXT_WAKE 0x1000
234#define CONTEXT_DEAD 0x0800
235#define CONTEXT_ACTIVE 0x0400
236
Stefan Richter8b7b6af2009-01-20 19:10:58 +0100237#define OHCI1394_MAX_AT_REQ_RETRIES 0xf
Kristian Høgsberged568912006-12-19 19:58:35 -0500238#define OHCI1394_MAX_AT_RESP_RETRIES 0x2
239#define OHCI1394_MAX_PHYS_RESP_RETRIES 0x8
240
Kristian Høgsberged568912006-12-19 19:58:35 -0500241#define OHCI1394_REGISTER_SIZE 0x800
242#define OHCI_LOOP_COUNT 500
243#define OHCI1394_PCI_HCI_Control 0x40
244#define SELF_ID_BUF_SIZE 0x800
Kristian Høgsberg32b46092007-02-06 14:49:30 -0500245#define OHCI_TCODE_PHY_PACKET 0x0e
Kristian Høgsberge364cf42007-02-16 17:34:49 -0500246#define OHCI_VERSION_1_1 0x010010
Kristian Høgsberg0edeefd2007-01-26 00:38:49 -0500247
Kristian Høgsberged568912006-12-19 19:58:35 -0500248static char ohci_driver_name[] = KBUILD_MODNAME;
249
Stefan Richterad3c0fe2008-03-20 22:04:36 +0100250#ifdef CONFIG_FIREWIRE_OHCI_DEBUG
251
Stefan Richtera007bb82008-04-07 22:33:35 +0200252#define OHCI_PARAM_DEBUG_AT_AR 1
Stefan Richterad3c0fe2008-03-20 22:04:36 +0100253#define OHCI_PARAM_DEBUG_SELFIDS 2
Stefan Richtera007bb82008-04-07 22:33:35 +0200254#define OHCI_PARAM_DEBUG_IRQS 4
255#define OHCI_PARAM_DEBUG_BUSRESETS 8 /* only effective before chip init */
Stefan Richterad3c0fe2008-03-20 22:04:36 +0100256
257static int param_debug;
258module_param_named(debug, param_debug, int, 0644);
259MODULE_PARM_DESC(debug, "Verbose logging (default = 0"
Stefan Richterad3c0fe2008-03-20 22:04:36 +0100260 ", AT/AR events = " __stringify(OHCI_PARAM_DEBUG_AT_AR)
Stefan Richtera007bb82008-04-07 22:33:35 +0200261 ", self-IDs = " __stringify(OHCI_PARAM_DEBUG_SELFIDS)
262 ", IRQs = " __stringify(OHCI_PARAM_DEBUG_IRQS)
263 ", busReset events = " __stringify(OHCI_PARAM_DEBUG_BUSRESETS)
Stefan Richterad3c0fe2008-03-20 22:04:36 +0100264 ", or a combination, or all = -1)");
265
266static void log_irqs(u32 evt)
267{
Stefan Richtera007bb82008-04-07 22:33:35 +0200268 if (likely(!(param_debug &
269 (OHCI_PARAM_DEBUG_IRQS | OHCI_PARAM_DEBUG_BUSRESETS))))
Stefan Richterad3c0fe2008-03-20 22:04:36 +0100270 return;
271
Stefan Richtera007bb82008-04-07 22:33:35 +0200272 if (!(param_debug & OHCI_PARAM_DEBUG_IRQS) &&
273 !(evt & OHCI1394_busReset))
274 return;
275
Stefan Richter161b96e2008-06-14 14:23:43 +0200276 fw_notify("IRQ %08x%s%s%s%s%s%s%s%s%s%s%s%s%s\n", evt,
277 evt & OHCI1394_selfIDComplete ? " selfID" : "",
278 evt & OHCI1394_RQPkt ? " AR_req" : "",
279 evt & OHCI1394_RSPkt ? " AR_resp" : "",
280 evt & OHCI1394_reqTxComplete ? " AT_req" : "",
281 evt & OHCI1394_respTxComplete ? " AT_resp" : "",
282 evt & OHCI1394_isochRx ? " IR" : "",
283 evt & OHCI1394_isochTx ? " IT" : "",
284 evt & OHCI1394_postedWriteErr ? " postedWriteErr" : "",
285 evt & OHCI1394_cycleTooLong ? " cycleTooLong" : "",
286 evt & OHCI1394_cycle64Seconds ? " cycle64Seconds" : "",
287 evt & OHCI1394_regAccessFail ? " regAccessFail" : "",
288 evt & OHCI1394_busReset ? " busReset" : "",
289 evt & ~(OHCI1394_selfIDComplete | OHCI1394_RQPkt |
290 OHCI1394_RSPkt | OHCI1394_reqTxComplete |
291 OHCI1394_respTxComplete | OHCI1394_isochRx |
292 OHCI1394_isochTx | OHCI1394_postedWriteErr |
293 OHCI1394_cycleTooLong | OHCI1394_cycle64Seconds |
294 OHCI1394_regAccessFail | OHCI1394_busReset)
Stefan Richterad3c0fe2008-03-20 22:04:36 +0100295 ? " ?" : "");
296}
297
298static const char *speed[] = {
299 [0] = "S100", [1] = "S200", [2] = "S400", [3] = "beta",
300};
301static const char *power[] = {
302 [0] = "+0W", [1] = "+15W", [2] = "+30W", [3] = "+45W",
303 [4] = "-3W", [5] = " ?W", [6] = "-3..-6W", [7] = "-3..-10W",
304};
305static const char port[] = { '.', '-', 'p', 'c', };
306
307static char _p(u32 *s, int shift)
308{
309 return port[*s >> shift & 3];
310}
311
Stefan Richter08ddb2f2008-04-11 00:51:15 +0200312static void log_selfids(int node_id, int generation, int self_id_count, u32 *s)
Stefan Richterad3c0fe2008-03-20 22:04:36 +0100313{
314 if (likely(!(param_debug & OHCI_PARAM_DEBUG_SELFIDS)))
315 return;
316
Stefan Richter161b96e2008-06-14 14:23:43 +0200317 fw_notify("%d selfIDs, generation %d, local node ID %04x\n",
318 self_id_count, generation, node_id);
Stefan Richterad3c0fe2008-03-20 22:04:36 +0100319
320 for (; self_id_count--; ++s)
321 if ((*s & 1 << 23) == 0)
Stefan Richter161b96e2008-06-14 14:23:43 +0200322 fw_notify("selfID 0: %08x, phy %d [%c%c%c] "
323 "%s gc=%d %s %s%s%s\n",
324 *s, *s >> 24 & 63, _p(s, 6), _p(s, 4), _p(s, 2),
325 speed[*s >> 14 & 3], *s >> 16 & 63,
326 power[*s >> 8 & 7], *s >> 22 & 1 ? "L" : "",
327 *s >> 11 & 1 ? "c" : "", *s & 2 ? "i" : "");
Stefan Richterad3c0fe2008-03-20 22:04:36 +0100328 else
Stefan Richter161b96e2008-06-14 14:23:43 +0200329 fw_notify("selfID n: %08x, phy %d [%c%c%c%c%c%c%c%c]\n",
330 *s, *s >> 24 & 63,
331 _p(s, 16), _p(s, 14), _p(s, 12), _p(s, 10),
332 _p(s, 8), _p(s, 6), _p(s, 4), _p(s, 2));
Stefan Richterad3c0fe2008-03-20 22:04:36 +0100333}
334
335static const char *evts[] = {
336 [0x00] = "evt_no_status", [0x01] = "-reserved-",
337 [0x02] = "evt_long_packet", [0x03] = "evt_missing_ack",
338 [0x04] = "evt_underrun", [0x05] = "evt_overrun",
339 [0x06] = "evt_descriptor_read", [0x07] = "evt_data_read",
340 [0x08] = "evt_data_write", [0x09] = "evt_bus_reset",
341 [0x0a] = "evt_timeout", [0x0b] = "evt_tcode_err",
342 [0x0c] = "-reserved-", [0x0d] = "-reserved-",
343 [0x0e] = "evt_unknown", [0x0f] = "evt_flushed",
344 [0x10] = "-reserved-", [0x11] = "ack_complete",
345 [0x12] = "ack_pending ", [0x13] = "-reserved-",
346 [0x14] = "ack_busy_X", [0x15] = "ack_busy_A",
347 [0x16] = "ack_busy_B", [0x17] = "-reserved-",
348 [0x18] = "-reserved-", [0x19] = "-reserved-",
349 [0x1a] = "-reserved-", [0x1b] = "ack_tardy",
350 [0x1c] = "-reserved-", [0x1d] = "ack_data_error",
351 [0x1e] = "ack_type_error", [0x1f] = "-reserved-",
352 [0x20] = "pending/cancelled",
353};
354static const char *tcodes[] = {
355 [0x0] = "QW req", [0x1] = "BW req",
356 [0x2] = "W resp", [0x3] = "-reserved-",
357 [0x4] = "QR req", [0x5] = "BR req",
358 [0x6] = "QR resp", [0x7] = "BR resp",
359 [0x8] = "cycle start", [0x9] = "Lk req",
360 [0xa] = "async stream packet", [0xb] = "Lk resp",
361 [0xc] = "-reserved-", [0xd] = "-reserved-",
362 [0xe] = "link internal", [0xf] = "-reserved-",
363};
364static const char *phys[] = {
365 [0x0] = "phy config packet", [0x1] = "link-on packet",
366 [0x2] = "self-id packet", [0x3] = "-reserved-",
367};
368
369static void log_ar_at_event(char dir, int speed, u32 *header, int evt)
370{
371 int tcode = header[0] >> 4 & 0xf;
372 char specific[12];
373
374 if (likely(!(param_debug & OHCI_PARAM_DEBUG_AT_AR)))
375 return;
376
377 if (unlikely(evt >= ARRAY_SIZE(evts)))
378 evt = 0x1f;
379
Stefan Richter08ddb2f2008-04-11 00:51:15 +0200380 if (evt == OHCI1394_evt_bus_reset) {
Stefan Richter161b96e2008-06-14 14:23:43 +0200381 fw_notify("A%c evt_bus_reset, generation %d\n",
382 dir, (header[2] >> 16) & 0xff);
Stefan Richter08ddb2f2008-04-11 00:51:15 +0200383 return;
384 }
385
Stefan Richterad3c0fe2008-03-20 22:04:36 +0100386 if (header[0] == ~header[1]) {
Stefan Richter161b96e2008-06-14 14:23:43 +0200387 fw_notify("A%c %s, %s, %08x\n",
388 dir, evts[evt], phys[header[0] >> 30 & 0x3], header[0]);
Stefan Richterad3c0fe2008-03-20 22:04:36 +0100389 return;
390 }
391
392 switch (tcode) {
393 case 0x0: case 0x6: case 0x8:
394 snprintf(specific, sizeof(specific), " = %08x",
395 be32_to_cpu((__force __be32)header[3]));
396 break;
397 case 0x1: case 0x5: case 0x7: case 0x9: case 0xb:
398 snprintf(specific, sizeof(specific), " %x,%x",
399 header[3] >> 16, header[3] & 0xffff);
400 break;
401 default:
402 specific[0] = '\0';
403 }
404
405 switch (tcode) {
406 case 0xe: case 0xa:
Stefan Richter161b96e2008-06-14 14:23:43 +0200407 fw_notify("A%c %s, %s\n", dir, evts[evt], tcodes[tcode]);
Stefan Richterad3c0fe2008-03-20 22:04:36 +0100408 break;
409 case 0x0: case 0x1: case 0x4: case 0x5: case 0x9:
Stefan Richter161b96e2008-06-14 14:23:43 +0200410 fw_notify("A%c spd %x tl %02x, "
411 "%04x -> %04x, %s, "
412 "%s, %04x%08x%s\n",
413 dir, speed, header[0] >> 10 & 0x3f,
414 header[1] >> 16, header[0] >> 16, evts[evt],
415 tcodes[tcode], header[1] & 0xffff, header[2], specific);
Stefan Richterad3c0fe2008-03-20 22:04:36 +0100416 break;
417 default:
Stefan Richter161b96e2008-06-14 14:23:43 +0200418 fw_notify("A%c spd %x tl %02x, "
419 "%04x -> %04x, %s, "
420 "%s%s\n",
421 dir, speed, header[0] >> 10 & 0x3f,
422 header[1] >> 16, header[0] >> 16, evts[evt],
423 tcodes[tcode], specific);
Stefan Richterad3c0fe2008-03-20 22:04:36 +0100424 }
425}
426
427#else
428
429#define log_irqs(evt)
Stefan Richter08ddb2f2008-04-11 00:51:15 +0200430#define log_selfids(node_id, generation, self_id_count, sid)
Stefan Richterad3c0fe2008-03-20 22:04:36 +0100431#define log_ar_at_event(dir, speed, header, evt)
432
433#endif /* CONFIG_FIREWIRE_OHCI_DEBUG */
434
Adrian Bunk95688e92007-01-22 19:17:37 +0100435static inline void reg_write(const struct fw_ohci *ohci, int offset, u32 data)
Kristian Høgsberged568912006-12-19 19:58:35 -0500436{
437 writel(data, ohci->registers + offset);
438}
439
Adrian Bunk95688e92007-01-22 19:17:37 +0100440static inline u32 reg_read(const struct fw_ohci *ohci, int offset)
Kristian Høgsberged568912006-12-19 19:58:35 -0500441{
442 return readl(ohci->registers + offset);
443}
444
Adrian Bunk95688e92007-01-22 19:17:37 +0100445static inline void flush_writes(const struct fw_ohci *ohci)
Kristian Høgsberged568912006-12-19 19:58:35 -0500446{
447 /* Do a dummy read to flush writes. */
448 reg_read(ohci, OHCI1394_Version);
449}
450
Stefan Richter53dca512008-12-14 21:47:04 +0100451static int ohci_update_phy_reg(struct fw_card *card, int addr,
452 int clear_bits, int set_bits)
Kristian Høgsberged568912006-12-19 19:58:35 -0500453{
454 struct fw_ohci *ohci = fw_ohci(card);
455 u32 val, old;
456
457 reg_write(ohci, OHCI1394_PhyControl, OHCI1394_PhyControl_Read(addr));
Stefan Richter362e9012007-07-12 22:24:19 +0200458 flush_writes(ohci);
Kristian Høgsberged568912006-12-19 19:58:35 -0500459 msleep(2);
460 val = reg_read(ohci, OHCI1394_PhyControl);
461 if ((val & OHCI1394_PhyControl_ReadDone) == 0) {
462 fw_error("failed to set phy reg bits.\n");
463 return -EBUSY;
464 }
465
466 old = OHCI1394_PhyControl_ReadData(val);
467 old = (old & ~clear_bits) | set_bits;
468 reg_write(ohci, OHCI1394_PhyControl,
469 OHCI1394_PhyControl_Write(addr, old));
470
471 return 0;
472}
473
Kristian Høgsberg32b46092007-02-06 14:49:30 -0500474static int ar_context_add_page(struct ar_context *ctx)
Kristian Høgsberged568912006-12-19 19:58:35 -0500475{
Kristian Høgsberg32b46092007-02-06 14:49:30 -0500476 struct device *dev = ctx->ohci->card.device;
477 struct ar_buffer *ab;
Stefan Richterf5101d582008-03-14 00:27:49 +0100478 dma_addr_t uninitialized_var(ab_bus);
Kristian Høgsberg32b46092007-02-06 14:49:30 -0500479 size_t offset;
480
Jarod Wilsonbde17092008-03-12 17:43:26 -0400481 ab = dma_alloc_coherent(dev, PAGE_SIZE, &ab_bus, GFP_ATOMIC);
Kristian Høgsberg32b46092007-02-06 14:49:30 -0500482 if (ab == NULL)
483 return -ENOMEM;
484
Jay Fenlasona55709b2008-10-22 15:59:42 -0400485 ab->next = NULL;
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -0400486 memset(&ab->descriptor, 0, sizeof(ab->descriptor));
Kristian Høgsberga77754a2007-05-07 20:33:35 -0400487 ab->descriptor.control = cpu_to_le16(DESCRIPTOR_INPUT_MORE |
488 DESCRIPTOR_STATUS |
489 DESCRIPTOR_BRANCH_ALWAYS);
Kristian Høgsberg32b46092007-02-06 14:49:30 -0500490 offset = offsetof(struct ar_buffer, data);
491 ab->descriptor.req_count = cpu_to_le16(PAGE_SIZE - offset);
492 ab->descriptor.data_address = cpu_to_le32(ab_bus + offset);
493 ab->descriptor.res_count = cpu_to_le16(PAGE_SIZE - offset);
494 ab->descriptor.branch_address = 0;
495
Kristian Høgsbergec839e42007-05-22 18:55:48 -0400496 ctx->last_buffer->descriptor.branch_address = cpu_to_le32(ab_bus | 1);
Kristian Høgsberg32b46092007-02-06 14:49:30 -0500497 ctx->last_buffer->next = ab;
498 ctx->last_buffer = ab;
499
Kristian Høgsberga77754a2007-05-07 20:33:35 -0400500 reg_write(ctx->ohci, CONTROL_SET(ctx->regs), CONTEXT_WAKE);
Kristian Høgsberged568912006-12-19 19:58:35 -0500501 flush_writes(ctx->ohci);
Kristian Høgsberg32b46092007-02-06 14:49:30 -0500502
503 return 0;
Kristian Høgsberged568912006-12-19 19:58:35 -0500504}
505
Jay Fenlasona55709b2008-10-22 15:59:42 -0400506static void ar_context_release(struct ar_context *ctx)
507{
508 struct ar_buffer *ab, *ab_next;
509 size_t offset;
510 dma_addr_t ab_bus;
511
512 for (ab = ctx->current_buffer; ab; ab = ab_next) {
513 ab_next = ab->next;
514 offset = offsetof(struct ar_buffer, data);
515 ab_bus = le32_to_cpu(ab->descriptor.data_address) - offset;
516 dma_free_coherent(ctx->ohci->card.device, PAGE_SIZE,
517 ab, ab_bus);
518 }
519}
520
Stefan Richter11bf20a2008-03-01 02:47:15 +0100521#if defined(CONFIG_PPC_PMAC) && defined(CONFIG_PPC32)
522#define cond_le32_to_cpu(v) \
523 (ohci->old_uninorth ? (__force __u32)(v) : le32_to_cpu(v))
524#else
525#define cond_le32_to_cpu(v) le32_to_cpu(v)
526#endif
527
Kristian Høgsberg32b46092007-02-06 14:49:30 -0500528static __le32 *handle_ar_packet(struct ar_context *ctx, __le32 *buffer)
Kristian Høgsberged568912006-12-19 19:58:35 -0500529{
Kristian Høgsberged568912006-12-19 19:58:35 -0500530 struct fw_ohci *ohci = ctx->ohci;
Kristian Høgsberg2639a6f2007-01-26 00:37:57 -0500531 struct fw_packet p;
532 u32 status, length, tcode;
Stefan Richter43286562008-03-11 21:22:26 +0100533 int evt;
Kristian Høgsberg0edeefd2007-01-26 00:38:49 -0500534
Stefan Richter11bf20a2008-03-01 02:47:15 +0100535 p.header[0] = cond_le32_to_cpu(buffer[0]);
536 p.header[1] = cond_le32_to_cpu(buffer[1]);
537 p.header[2] = cond_le32_to_cpu(buffer[2]);
Kristian Høgsberg2639a6f2007-01-26 00:37:57 -0500538
539 tcode = (p.header[0] >> 4) & 0x0f;
540 switch (tcode) {
541 case TCODE_WRITE_QUADLET_REQUEST:
542 case TCODE_READ_QUADLET_RESPONSE:
Kristian Høgsberg32b46092007-02-06 14:49:30 -0500543 p.header[3] = (__force __u32) buffer[3];
Kristian Høgsberg2639a6f2007-01-26 00:37:57 -0500544 p.header_length = 16;
Kristian Høgsberg32b46092007-02-06 14:49:30 -0500545 p.payload_length = 0;
546 break;
547
548 case TCODE_READ_BLOCK_REQUEST :
Stefan Richter11bf20a2008-03-01 02:47:15 +0100549 p.header[3] = cond_le32_to_cpu(buffer[3]);
Kristian Høgsberg32b46092007-02-06 14:49:30 -0500550 p.header_length = 16;
551 p.payload_length = 0;
Kristian Høgsberg2639a6f2007-01-26 00:37:57 -0500552 break;
553
554 case TCODE_WRITE_BLOCK_REQUEST:
Kristian Høgsberg2639a6f2007-01-26 00:37:57 -0500555 case TCODE_READ_BLOCK_RESPONSE:
556 case TCODE_LOCK_REQUEST:
557 case TCODE_LOCK_RESPONSE:
Stefan Richter11bf20a2008-03-01 02:47:15 +0100558 p.header[3] = cond_le32_to_cpu(buffer[3]);
Kristian Høgsberg2639a6f2007-01-26 00:37:57 -0500559 p.header_length = 16;
Kristian Høgsberg32b46092007-02-06 14:49:30 -0500560 p.payload_length = p.header[3] >> 16;
Kristian Høgsberg2639a6f2007-01-26 00:37:57 -0500561 break;
562
563 case TCODE_WRITE_RESPONSE:
564 case TCODE_READ_QUADLET_REQUEST:
Kristian Høgsberg32b46092007-02-06 14:49:30 -0500565 case OHCI_TCODE_PHY_PACKET:
Kristian Høgsberg2639a6f2007-01-26 00:37:57 -0500566 p.header_length = 12;
Kristian Høgsberg32b46092007-02-06 14:49:30 -0500567 p.payload_length = 0;
Kristian Høgsberg2639a6f2007-01-26 00:37:57 -0500568 break;
Stefan Richterccff9622008-05-31 19:36:06 +0200569
570 default:
571 /* FIXME: Stop context, discard everything, and restart? */
572 p.header_length = 0;
573 p.payload_length = 0;
Kristian Høgsberg2639a6f2007-01-26 00:37:57 -0500574 }
575
Kristian Høgsberg32b46092007-02-06 14:49:30 -0500576 p.payload = (void *) buffer + p.header_length;
577
578 /* FIXME: What to do about evt_* errors? */
579 length = (p.header_length + p.payload_length + 3) / 4;
Stefan Richter11bf20a2008-03-01 02:47:15 +0100580 status = cond_le32_to_cpu(buffer[length]);
Stefan Richter43286562008-03-11 21:22:26 +0100581 evt = (status >> 16) & 0x1f;
Kristian Høgsberg32b46092007-02-06 14:49:30 -0500582
Stefan Richter43286562008-03-11 21:22:26 +0100583 p.ack = evt - 16;
Kristian Høgsberg32b46092007-02-06 14:49:30 -0500584 p.speed = (status >> 21) & 0x7;
585 p.timestamp = status & 0xffff;
586 p.generation = ohci->request_generation;
Kristian Høgsberged568912006-12-19 19:58:35 -0500587
Stefan Richter43286562008-03-11 21:22:26 +0100588 log_ar_at_event('R', p.speed, p.header, evt);
Stefan Richterad3c0fe2008-03-20 22:04:36 +0100589
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400590 /*
591 * The OHCI bus reset handler synthesizes a phy packet with
Kristian Høgsberged568912006-12-19 19:58:35 -0500592 * the new generation number when a bus reset happens (see
593 * section 8.4.2.3). This helps us determine when a request
594 * was received and make sure we send the response in the same
595 * generation. We only need this for requests; for responses
596 * we use the unique tlabel for finding the matching
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400597 * request.
Stefan Richterd34316a2008-04-12 22:31:25 +0200598 *
599 * Alas some chips sometimes emit bus reset packets with a
600 * wrong generation. We set the correct generation for these
601 * at a slightly incorrect time (in bus_reset_tasklet).
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400602 */
Stefan Richterd34316a2008-04-12 22:31:25 +0200603 if (evt == OHCI1394_evt_bus_reset) {
604 if (!ohci->bus_reset_packet_quirk)
605 ohci->request_generation = (p.header[2] >> 16) & 0xff;
606 } else if (ctx == &ohci->ar_request_ctx) {
Kristian Høgsberg2639a6f2007-01-26 00:37:57 -0500607 fw_core_handle_request(&ohci->card, &p);
Stefan Richterd34316a2008-04-12 22:31:25 +0200608 } else {
Kristian Høgsberg2639a6f2007-01-26 00:37:57 -0500609 fw_core_handle_response(&ohci->card, &p);
Stefan Richterd34316a2008-04-12 22:31:25 +0200610 }
Kristian Høgsberged568912006-12-19 19:58:35 -0500611
Kristian Høgsberg32b46092007-02-06 14:49:30 -0500612 return buffer + length + 1;
613}
Kristian Høgsberged568912006-12-19 19:58:35 -0500614
Kristian Høgsberg32b46092007-02-06 14:49:30 -0500615static void ar_context_tasklet(unsigned long data)
616{
617 struct ar_context *ctx = (struct ar_context *)data;
618 struct fw_ohci *ohci = ctx->ohci;
619 struct ar_buffer *ab;
620 struct descriptor *d;
621 void *buffer, *end;
Kristian Høgsberged568912006-12-19 19:58:35 -0500622
Kristian Høgsberg32b46092007-02-06 14:49:30 -0500623 ab = ctx->current_buffer;
624 d = &ab->descriptor;
Kristian Høgsberged568912006-12-19 19:58:35 -0500625
Kristian Høgsberg32b46092007-02-06 14:49:30 -0500626 if (d->res_count == 0) {
627 size_t size, rest, offset;
Jarod Wilson6b842362008-03-25 16:47:16 -0400628 dma_addr_t start_bus;
629 void *start;
Kristian Høgsberg32b46092007-02-06 14:49:30 -0500630
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400631 /*
632 * This descriptor is finished and we may have a
Kristian Høgsberg32b46092007-02-06 14:49:30 -0500633 * packet split across this and the next buffer. We
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400634 * reuse the page for reassembling the split packet.
635 */
Kristian Høgsberg32b46092007-02-06 14:49:30 -0500636
637 offset = offsetof(struct ar_buffer, data);
Jarod Wilson6b842362008-03-25 16:47:16 -0400638 start = buffer = ab;
639 start_bus = le32_to_cpu(ab->descriptor.data_address) - offset;
Kristian Høgsberg32b46092007-02-06 14:49:30 -0500640
Kristian Høgsberg32b46092007-02-06 14:49:30 -0500641 ab = ab->next;
642 d = &ab->descriptor;
643 size = buffer + PAGE_SIZE - ctx->pointer;
644 rest = le16_to_cpu(d->req_count) - le16_to_cpu(d->res_count);
645 memmove(buffer, ctx->pointer, size);
646 memcpy(buffer + size, ab->data, rest);
647 ctx->current_buffer = ab;
648 ctx->pointer = (void *) ab->data + rest;
649 end = buffer + size + rest;
650
651 while (buffer < end)
652 buffer = handle_ar_packet(ctx, buffer);
653
Jarod Wilsonbde17092008-03-12 17:43:26 -0400654 dma_free_coherent(ohci->card.device, PAGE_SIZE,
Jarod Wilson6b842362008-03-25 16:47:16 -0400655 start, start_bus);
Kristian Høgsberg32b46092007-02-06 14:49:30 -0500656 ar_context_add_page(ctx);
657 } else {
658 buffer = ctx->pointer;
659 ctx->pointer = end =
660 (void *) ab + PAGE_SIZE - le16_to_cpu(d->res_count);
661
662 while (buffer < end)
663 buffer = handle_ar_packet(ctx, buffer);
664 }
Kristian Høgsberged568912006-12-19 19:58:35 -0500665}
666
Stefan Richter53dca512008-12-14 21:47:04 +0100667static int ar_context_init(struct ar_context *ctx,
668 struct fw_ohci *ohci, u32 regs)
Kristian Høgsberged568912006-12-19 19:58:35 -0500669{
Kristian Høgsberg32b46092007-02-06 14:49:30 -0500670 struct ar_buffer ab;
Kristian Høgsberged568912006-12-19 19:58:35 -0500671
Kristian Høgsberg72e318e2007-02-06 14:49:31 -0500672 ctx->regs = regs;
673 ctx->ohci = ohci;
674 ctx->last_buffer = &ab;
Kristian Høgsberged568912006-12-19 19:58:35 -0500675 tasklet_init(&ctx->tasklet, ar_context_tasklet, (unsigned long)ctx);
676
Kristian Høgsberg32b46092007-02-06 14:49:30 -0500677 ar_context_add_page(ctx);
678 ar_context_add_page(ctx);
679 ctx->current_buffer = ab.next;
680 ctx->pointer = ctx->current_buffer->data;
681
Kristian Høgsberg2aef4692007-05-30 19:06:35 -0400682 return 0;
683}
684
685static void ar_context_run(struct ar_context *ctx)
686{
687 struct ar_buffer *ab = ctx->current_buffer;
688 dma_addr_t ab_bus;
689 size_t offset;
690
691 offset = offsetof(struct ar_buffer, data);
Stefan Richter0a9972b2007-06-23 20:28:17 +0200692 ab_bus = le32_to_cpu(ab->descriptor.data_address) - offset;
Kristian Høgsberg2aef4692007-05-30 19:06:35 -0400693
694 reg_write(ctx->ohci, COMMAND_PTR(ctx->regs), ab_bus | 1);
Kristian Høgsberga77754a2007-05-07 20:33:35 -0400695 reg_write(ctx->ohci, CONTROL_SET(ctx->regs), CONTEXT_RUN);
Kristian Høgsberg32b46092007-02-06 14:49:30 -0500696 flush_writes(ctx->ohci);
Kristian Høgsberged568912006-12-19 19:58:35 -0500697}
Stefan Richter373b2ed2007-03-04 14:45:18 +0100698
Stefan Richter53dca512008-12-14 21:47:04 +0100699static struct descriptor *find_branch_descriptor(struct descriptor *d, int z)
Jarod Wilsona186b4a2007-12-03 13:43:12 -0500700{
701 int b, key;
702
703 b = (le16_to_cpu(d->control) & DESCRIPTOR_BRANCH_ALWAYS) >> 2;
704 key = (le16_to_cpu(d->control) & DESCRIPTOR_KEY_IMMEDIATE) >> 8;
705
706 /* figure out which descriptor the branch address goes in */
707 if (z == 2 && (b == 3 || key == 2))
708 return d;
709 else
710 return d + z - 1;
711}
712
Kristian Høgsberg30200732007-02-16 17:34:39 -0500713static void context_tasklet(unsigned long data)
714{
715 struct context *ctx = (struct context *) data;
Kristian Høgsberg30200732007-02-16 17:34:39 -0500716 struct descriptor *d, *last;
717 u32 address;
718 int z;
David Moorefe5ca632008-01-06 17:21:41 -0500719 struct descriptor_buffer *desc;
Kristian Høgsberg30200732007-02-16 17:34:39 -0500720
David Moorefe5ca632008-01-06 17:21:41 -0500721 desc = list_entry(ctx->buffer_list.next,
722 struct descriptor_buffer, list);
723 last = ctx->last;
Kristian Høgsberg30200732007-02-16 17:34:39 -0500724 while (last->branch_address != 0) {
David Moorefe5ca632008-01-06 17:21:41 -0500725 struct descriptor_buffer *old_desc = desc;
Kristian Høgsberg30200732007-02-16 17:34:39 -0500726 address = le32_to_cpu(last->branch_address);
727 z = address & 0xf;
David Moorefe5ca632008-01-06 17:21:41 -0500728 address &= ~0xf;
729
730 /* If the branch address points to a buffer outside of the
731 * current buffer, advance to the next buffer. */
732 if (address < desc->buffer_bus ||
733 address >= desc->buffer_bus + desc->used)
734 desc = list_entry(desc->list.next,
735 struct descriptor_buffer, list);
736 d = desc->buffer + (address - desc->buffer_bus) / sizeof(*d);
Jarod Wilsona186b4a2007-12-03 13:43:12 -0500737 last = find_branch_descriptor(d, z);
Kristian Høgsberg30200732007-02-16 17:34:39 -0500738
739 if (!ctx->callback(ctx, d, last))
740 break;
741
David Moorefe5ca632008-01-06 17:21:41 -0500742 if (old_desc != desc) {
743 /* If we've advanced to the next buffer, move the
744 * previous buffer to the free list. */
745 unsigned long flags;
746 old_desc->used = 0;
747 spin_lock_irqsave(&ctx->ohci->lock, flags);
748 list_move_tail(&old_desc->list, &ctx->buffer_list);
749 spin_unlock_irqrestore(&ctx->ohci->lock, flags);
750 }
751 ctx->last = last;
Kristian Høgsberg30200732007-02-16 17:34:39 -0500752 }
753}
754
David Moorefe5ca632008-01-06 17:21:41 -0500755/*
756 * Allocate a new buffer and add it to the list of free buffers for this
757 * context. Must be called with ohci->lock held.
758 */
Stefan Richter53dca512008-12-14 21:47:04 +0100759static int context_add_buffer(struct context *ctx)
David Moorefe5ca632008-01-06 17:21:41 -0500760{
761 struct descriptor_buffer *desc;
Stefan Richterf5101d582008-03-14 00:27:49 +0100762 dma_addr_t uninitialized_var(bus_addr);
David Moorefe5ca632008-01-06 17:21:41 -0500763 int offset;
764
765 /*
766 * 16MB of descriptors should be far more than enough for any DMA
767 * program. This will catch run-away userspace or DoS attacks.
768 */
769 if (ctx->total_allocation >= 16*1024*1024)
770 return -ENOMEM;
771
772 desc = dma_alloc_coherent(ctx->ohci->card.device, PAGE_SIZE,
773 &bus_addr, GFP_ATOMIC);
774 if (!desc)
775 return -ENOMEM;
776
777 offset = (void *)&desc->buffer - (void *)desc;
778 desc->buffer_size = PAGE_SIZE - offset;
779 desc->buffer_bus = bus_addr + offset;
780 desc->used = 0;
781
782 list_add_tail(&desc->list, &ctx->buffer_list);
783 ctx->total_allocation += PAGE_SIZE;
784
785 return 0;
786}
787
Stefan Richter53dca512008-12-14 21:47:04 +0100788static int context_init(struct context *ctx, struct fw_ohci *ohci,
789 u32 regs, descriptor_callback_t callback)
Kristian Høgsberg30200732007-02-16 17:34:39 -0500790{
791 ctx->ohci = ohci;
792 ctx->regs = regs;
David Moorefe5ca632008-01-06 17:21:41 -0500793 ctx->total_allocation = 0;
794
795 INIT_LIST_HEAD(&ctx->buffer_list);
796 if (context_add_buffer(ctx) < 0)
Kristian Høgsberg30200732007-02-16 17:34:39 -0500797 return -ENOMEM;
798
David Moorefe5ca632008-01-06 17:21:41 -0500799 ctx->buffer_tail = list_entry(ctx->buffer_list.next,
800 struct descriptor_buffer, list);
801
Kristian Høgsberg30200732007-02-16 17:34:39 -0500802 tasklet_init(&ctx->tasklet, context_tasklet, (unsigned long)ctx);
803 ctx->callback = callback;
804
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400805 /*
806 * We put a dummy descriptor in the buffer that has a NULL
Kristian Høgsberg30200732007-02-16 17:34:39 -0500807 * branch address and looks like it's been sent. That way we
David Moorefe5ca632008-01-06 17:21:41 -0500808 * have a descriptor to append DMA programs to.
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400809 */
David Moorefe5ca632008-01-06 17:21:41 -0500810 memset(ctx->buffer_tail->buffer, 0, sizeof(*ctx->buffer_tail->buffer));
811 ctx->buffer_tail->buffer->control = cpu_to_le16(DESCRIPTOR_OUTPUT_LAST);
812 ctx->buffer_tail->buffer->transfer_status = cpu_to_le16(0x8011);
813 ctx->buffer_tail->used += sizeof(*ctx->buffer_tail->buffer);
814 ctx->last = ctx->buffer_tail->buffer;
815 ctx->prev = ctx->buffer_tail->buffer;
Kristian Høgsberg30200732007-02-16 17:34:39 -0500816
817 return 0;
818}
819
Stefan Richter53dca512008-12-14 21:47:04 +0100820static void context_release(struct context *ctx)
Kristian Høgsberg30200732007-02-16 17:34:39 -0500821{
822 struct fw_card *card = &ctx->ohci->card;
David Moorefe5ca632008-01-06 17:21:41 -0500823 struct descriptor_buffer *desc, *tmp;
Kristian Høgsberg30200732007-02-16 17:34:39 -0500824
David Moorefe5ca632008-01-06 17:21:41 -0500825 list_for_each_entry_safe(desc, tmp, &ctx->buffer_list, list)
826 dma_free_coherent(card->device, PAGE_SIZE, desc,
827 desc->buffer_bus -
828 ((void *)&desc->buffer - (void *)desc));
Kristian Høgsberg30200732007-02-16 17:34:39 -0500829}
830
David Moorefe5ca632008-01-06 17:21:41 -0500831/* Must be called with ohci->lock held */
Stefan Richter53dca512008-12-14 21:47:04 +0100832static struct descriptor *context_get_descriptors(struct context *ctx,
833 int z, dma_addr_t *d_bus)
Kristian Høgsberg30200732007-02-16 17:34:39 -0500834{
David Moorefe5ca632008-01-06 17:21:41 -0500835 struct descriptor *d = NULL;
836 struct descriptor_buffer *desc = ctx->buffer_tail;
Kristian Høgsberg30200732007-02-16 17:34:39 -0500837
David Moorefe5ca632008-01-06 17:21:41 -0500838 if (z * sizeof(*d) > desc->buffer_size)
839 return NULL;
Kristian Høgsberg30200732007-02-16 17:34:39 -0500840
David Moorefe5ca632008-01-06 17:21:41 -0500841 if (z * sizeof(*d) > desc->buffer_size - desc->used) {
842 /* No room for the descriptor in this buffer, so advance to the
843 * next one. */
844
845 if (desc->list.next == &ctx->buffer_list) {
846 /* If there is no free buffer next in the list,
847 * allocate one. */
848 if (context_add_buffer(ctx) < 0)
849 return NULL;
850 }
851 desc = list_entry(desc->list.next,
852 struct descriptor_buffer, list);
853 ctx->buffer_tail = desc;
Kristian Høgsberg30200732007-02-16 17:34:39 -0500854 }
855
David Moorefe5ca632008-01-06 17:21:41 -0500856 d = desc->buffer + desc->used / sizeof(*d);
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -0400857 memset(d, 0, z * sizeof(*d));
David Moorefe5ca632008-01-06 17:21:41 -0500858 *d_bus = desc->buffer_bus + desc->used;
Kristian Høgsberg30200732007-02-16 17:34:39 -0500859
860 return d;
861}
862
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -0500863static void context_run(struct context *ctx, u32 extra)
Kristian Høgsberg30200732007-02-16 17:34:39 -0500864{
865 struct fw_ohci *ohci = ctx->ohci;
866
Kristian Høgsberga77754a2007-05-07 20:33:35 -0400867 reg_write(ohci, COMMAND_PTR(ctx->regs),
David Moorefe5ca632008-01-06 17:21:41 -0500868 le32_to_cpu(ctx->last->branch_address));
Kristian Høgsberga77754a2007-05-07 20:33:35 -0400869 reg_write(ohci, CONTROL_CLEAR(ctx->regs), ~0);
870 reg_write(ohci, CONTROL_SET(ctx->regs), CONTEXT_RUN | extra);
Kristian Høgsberg30200732007-02-16 17:34:39 -0500871 flush_writes(ohci);
872}
873
874static void context_append(struct context *ctx,
875 struct descriptor *d, int z, int extra)
876{
877 dma_addr_t d_bus;
David Moorefe5ca632008-01-06 17:21:41 -0500878 struct descriptor_buffer *desc = ctx->buffer_tail;
Kristian Høgsberg30200732007-02-16 17:34:39 -0500879
David Moorefe5ca632008-01-06 17:21:41 -0500880 d_bus = desc->buffer_bus + (d - desc->buffer) * sizeof(*d);
Kristian Høgsberg30200732007-02-16 17:34:39 -0500881
David Moorefe5ca632008-01-06 17:21:41 -0500882 desc->used += (z + extra) * sizeof(*d);
883 ctx->prev->branch_address = cpu_to_le32(d_bus | z);
884 ctx->prev = find_branch_descriptor(d, z);
Kristian Høgsberg30200732007-02-16 17:34:39 -0500885
Kristian Høgsberga77754a2007-05-07 20:33:35 -0400886 reg_write(ctx->ohci, CONTROL_SET(ctx->regs), CONTEXT_WAKE);
Kristian Høgsberg30200732007-02-16 17:34:39 -0500887 flush_writes(ctx->ohci);
888}
889
890static void context_stop(struct context *ctx)
891{
892 u32 reg;
Kristian Høgsbergb8295662007-02-16 17:34:42 -0500893 int i;
Kristian Høgsberg30200732007-02-16 17:34:39 -0500894
Kristian Høgsberga77754a2007-05-07 20:33:35 -0400895 reg_write(ctx->ohci, CONTROL_CLEAR(ctx->regs), CONTEXT_RUN);
Kristian Høgsbergb8295662007-02-16 17:34:42 -0500896 flush_writes(ctx->ohci);
Kristian Høgsberg30200732007-02-16 17:34:39 -0500897
Kristian Høgsbergb8295662007-02-16 17:34:42 -0500898 for (i = 0; i < 10; i++) {
Kristian Høgsberga77754a2007-05-07 20:33:35 -0400899 reg = reg_read(ctx->ohci, CONTROL_SET(ctx->regs));
Kristian Høgsbergb8295662007-02-16 17:34:42 -0500900 if ((reg & CONTEXT_ACTIVE) == 0)
Stefan Richterb0068542009-01-05 20:43:23 +0100901 return;
Kristian Høgsbergb8295662007-02-16 17:34:42 -0500902
Stefan Richterb980f5a2007-07-12 22:25:14 +0200903 mdelay(1);
Kristian Høgsbergb8295662007-02-16 17:34:42 -0500904 }
Stefan Richterb0068542009-01-05 20:43:23 +0100905 fw_error("Error: DMA context still active (0x%08x)\n", reg);
Kristian Høgsberg30200732007-02-16 17:34:39 -0500906}
Kristian Høgsberged568912006-12-19 19:58:35 -0500907
Kristian Høgsbergf319b6a2007-03-07 12:12:49 -0500908struct driver_data {
Kristian Høgsberged568912006-12-19 19:58:35 -0500909 struct fw_packet *packet;
Kristian Høgsbergf319b6a2007-03-07 12:12:49 -0500910};
911
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400912/*
913 * This function apppends a packet to the DMA queue for transmission.
Kristian Høgsbergf319b6a2007-03-07 12:12:49 -0500914 * Must always be called with the ochi->lock held to ensure proper
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400915 * generation handling and locking around packet queue manipulation.
916 */
Stefan Richter53dca512008-12-14 21:47:04 +0100917static int at_context_queue_packet(struct context *ctx,
918 struct fw_packet *packet)
Kristian Høgsbergf319b6a2007-03-07 12:12:49 -0500919{
Kristian Høgsberged568912006-12-19 19:58:35 -0500920 struct fw_ohci *ohci = ctx->ohci;
Stefan Richter4b6d51e2007-10-21 11:20:07 +0200921 dma_addr_t d_bus, uninitialized_var(payload_bus);
Kristian Høgsbergf319b6a2007-03-07 12:12:49 -0500922 struct driver_data *driver_data;
923 struct descriptor *d, *last;
924 __le32 *header;
Kristian Høgsberged568912006-12-19 19:58:35 -0500925 int z, tcode;
Kristian Høgsbergf319b6a2007-03-07 12:12:49 -0500926 u32 reg;
Kristian Høgsberged568912006-12-19 19:58:35 -0500927
Kristian Høgsbergf319b6a2007-03-07 12:12:49 -0500928 d = context_get_descriptors(ctx, 4, &d_bus);
929 if (d == NULL) {
930 packet->ack = RCODE_SEND_ERROR;
931 return -1;
Kristian Høgsberged568912006-12-19 19:58:35 -0500932 }
933
Kristian Høgsberga77754a2007-05-07 20:33:35 -0400934 d[0].control = cpu_to_le16(DESCRIPTOR_KEY_IMMEDIATE);
Kristian Høgsbergf319b6a2007-03-07 12:12:49 -0500935 d[0].res_count = cpu_to_le16(packet->timestamp);
936
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400937 /*
938 * The DMA format for asyncronous link packets is different
Kristian Høgsberged568912006-12-19 19:58:35 -0500939 * from the IEEE1394 layout, so shift the fields around
940 * accordingly. If header_length is 8, it's a PHY packet, to
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400941 * which we need to prepend an extra quadlet.
942 */
Kristian Høgsbergf319b6a2007-03-07 12:12:49 -0500943
944 header = (__le32 *) &d[1];
Jay Fenlasonf8c22872009-03-05 19:08:40 +0100945 switch (packet->header_length) {
946 case 16:
947 case 12:
Kristian Høgsbergf319b6a2007-03-07 12:12:49 -0500948 header[0] = cpu_to_le32((packet->header[0] & 0xffff) |
949 (packet->speed << 16));
950 header[1] = cpu_to_le32((packet->header[1] & 0xffff) |
951 (packet->header[0] & 0xffff0000));
952 header[2] = cpu_to_le32(packet->header[2]);
Kristian Høgsberged568912006-12-19 19:58:35 -0500953
954 tcode = (packet->header[0] >> 4) & 0x0f;
955 if (TCODE_IS_BLOCK_PACKET(tcode))
Kristian Høgsbergf319b6a2007-03-07 12:12:49 -0500956 header[3] = cpu_to_le32(packet->header[3]);
Kristian Høgsberged568912006-12-19 19:58:35 -0500957 else
Kristian Høgsbergf319b6a2007-03-07 12:12:49 -0500958 header[3] = (__force __le32) packet->header[3];
959
960 d[0].req_count = cpu_to_le16(packet->header_length);
Jay Fenlasonf8c22872009-03-05 19:08:40 +0100961 break;
962
963 case 8:
Kristian Høgsbergf319b6a2007-03-07 12:12:49 -0500964 header[0] = cpu_to_le32((OHCI1394_phy_tcode << 4) |
965 (packet->speed << 16));
966 header[1] = cpu_to_le32(packet->header[0]);
967 header[2] = cpu_to_le32(packet->header[1]);
968 d[0].req_count = cpu_to_le16(12);
Jay Fenlasonf8c22872009-03-05 19:08:40 +0100969 break;
970
971 case 4:
972 header[0] = cpu_to_le32((packet->header[0] & 0xffff) |
973 (packet->speed << 16));
974 header[1] = cpu_to_le32(packet->header[0] & 0xffff0000);
975 d[0].req_count = cpu_to_le16(8);
976 break;
977
978 default:
979 /* BUG(); */
980 packet->ack = RCODE_SEND_ERROR;
981 return -1;
Kristian Høgsberged568912006-12-19 19:58:35 -0500982 }
983
Kristian Høgsbergf319b6a2007-03-07 12:12:49 -0500984 driver_data = (struct driver_data *) &d[3];
985 driver_data->packet = packet;
Kristian Høgsberg20d11672007-03-26 19:18:19 -0400986 packet->driver_data = driver_data;
Jarod Wilsona186b4a2007-12-03 13:43:12 -0500987
Kristian Høgsbergf319b6a2007-03-07 12:12:49 -0500988 if (packet->payload_length > 0) {
989 payload_bus =
990 dma_map_single(ohci->card.device, packet->payload,
991 packet->payload_length, DMA_TO_DEVICE);
FUJITA Tomonori8d8bb392008-07-25 19:44:49 -0700992 if (dma_mapping_error(ohci->card.device, payload_bus)) {
Kristian Høgsbergf319b6a2007-03-07 12:12:49 -0500993 packet->ack = RCODE_SEND_ERROR;
994 return -1;
995 }
Stefan Richter1d1dc5e2008-12-10 00:20:38 +0100996 packet->payload_bus = payload_bus;
Kristian Høgsbergf319b6a2007-03-07 12:12:49 -0500997
998 d[2].req_count = cpu_to_le16(packet->payload_length);
999 d[2].data_address = cpu_to_le32(payload_bus);
1000 last = &d[2];
1001 z = 3;
1002 } else {
1003 last = &d[0];
1004 z = 2;
1005 }
1006
Kristian Høgsberga77754a2007-05-07 20:33:35 -04001007 last->control |= cpu_to_le16(DESCRIPTOR_OUTPUT_LAST |
1008 DESCRIPTOR_IRQ_ALWAYS |
1009 DESCRIPTOR_BRANCH_ALWAYS);
Kristian Høgsbergf319b6a2007-03-07 12:12:49 -05001010
Jarod Wilson76f73ca2008-04-07 22:32:33 +02001011 /*
1012 * If the controller and packet generations don't match, we need to
1013 * bail out and try again. If IntEvent.busReset is set, the AT context
1014 * is halted, so appending to the context and trying to run it is
1015 * futile. Most controllers do the right thing and just flush the AT
1016 * queue (per section 7.2.3.2 of the OHCI 1.1 specification), but
1017 * some controllers (like a JMicron JMB381 PCI-e) misbehave and wind
1018 * up stalling out. So we just bail out in software and try again
1019 * later, and everyone is happy.
1020 * FIXME: Document how the locking works.
1021 */
1022 if (ohci->generation != packet->generation ||
1023 reg_read(ohci, OHCI1394_IntEventSet) & OHCI1394_busReset) {
Stefan Richterab88ca42007-08-29 19:40:28 +02001024 if (packet->payload_length > 0)
1025 dma_unmap_single(ohci->card.device, payload_bus,
1026 packet->payload_length, DMA_TO_DEVICE);
Kristian Høgsbergf319b6a2007-03-07 12:12:49 -05001027 packet->ack = RCODE_GENERATION;
1028 return -1;
Kristian Høgsberged568912006-12-19 19:58:35 -05001029 }
Kristian Høgsberged568912006-12-19 19:58:35 -05001030
Kristian Høgsbergf319b6a2007-03-07 12:12:49 -05001031 context_append(ctx, d, z, 4 - z);
Kristian Høgsberged568912006-12-19 19:58:35 -05001032
Kristian Høgsbergf319b6a2007-03-07 12:12:49 -05001033 /* If the context isn't already running, start it up. */
Kristian Høgsberga77754a2007-05-07 20:33:35 -04001034 reg = reg_read(ctx->ohci, CONTROL_SET(ctx->regs));
Kristian Høgsberg053b3082007-04-10 18:11:17 -04001035 if ((reg & CONTEXT_RUN) == 0)
Kristian Høgsbergf319b6a2007-03-07 12:12:49 -05001036 context_run(ctx, 0);
Kristian Høgsberged568912006-12-19 19:58:35 -05001037
1038 return 0;
1039}
1040
Kristian Høgsbergf319b6a2007-03-07 12:12:49 -05001041static int handle_at_packet(struct context *context,
1042 struct descriptor *d,
1043 struct descriptor *last)
1044{
1045 struct driver_data *driver_data;
1046 struct fw_packet *packet;
1047 struct fw_ohci *ohci = context->ohci;
Kristian Høgsbergf319b6a2007-03-07 12:12:49 -05001048 int evt;
1049
1050 if (last->transfer_status == 0)
1051 /* This descriptor isn't done yet, stop iteration. */
1052 return 0;
1053
1054 driver_data = (struct driver_data *) &d[3];
1055 packet = driver_data->packet;
1056 if (packet == NULL)
1057 /* This packet was cancelled, just continue. */
1058 return 1;
1059
Stefan Richter1d1dc5e2008-12-10 00:20:38 +01001060 if (packet->payload_bus)
1061 dma_unmap_single(ohci->card.device, packet->payload_bus,
Kristian Høgsbergf319b6a2007-03-07 12:12:49 -05001062 packet->payload_length, DMA_TO_DEVICE);
1063
1064 evt = le16_to_cpu(last->transfer_status) & 0x1f;
1065 packet->timestamp = le16_to_cpu(last->res_count);
1066
Stefan Richterad3c0fe2008-03-20 22:04:36 +01001067 log_ar_at_event('T', packet->speed, packet->header, evt);
1068
Kristian Høgsbergf319b6a2007-03-07 12:12:49 -05001069 switch (evt) {
1070 case OHCI1394_evt_timeout:
1071 /* Async response transmit timed out. */
1072 packet->ack = RCODE_CANCELLED;
1073 break;
1074
1075 case OHCI1394_evt_flushed:
Kristian Høgsbergc781c062007-05-07 20:33:32 -04001076 /*
1077 * The packet was flushed should give same error as
1078 * when we try to use a stale generation count.
1079 */
Kristian Høgsbergf319b6a2007-03-07 12:12:49 -05001080 packet->ack = RCODE_GENERATION;
1081 break;
1082
1083 case OHCI1394_evt_missing_ack:
Kristian Høgsbergc781c062007-05-07 20:33:32 -04001084 /*
1085 * Using a valid (current) generation count, but the
1086 * node is not on the bus or not sending acks.
1087 */
Kristian Høgsbergf319b6a2007-03-07 12:12:49 -05001088 packet->ack = RCODE_NO_ACK;
1089 break;
1090
1091 case ACK_COMPLETE + 0x10:
1092 case ACK_PENDING + 0x10:
1093 case ACK_BUSY_X + 0x10:
1094 case ACK_BUSY_A + 0x10:
1095 case ACK_BUSY_B + 0x10:
1096 case ACK_DATA_ERROR + 0x10:
1097 case ACK_TYPE_ERROR + 0x10:
1098 packet->ack = evt - 0x10;
1099 break;
1100
1101 default:
1102 packet->ack = RCODE_SEND_ERROR;
1103 break;
1104 }
1105
1106 packet->callback(packet, &ohci->card, packet->ack);
1107
1108 return 1;
1109}
1110
Kristian Høgsberga77754a2007-05-07 20:33:35 -04001111#define HEADER_GET_DESTINATION(q) (((q) >> 16) & 0xffff)
1112#define HEADER_GET_TCODE(q) (((q) >> 4) & 0x0f)
1113#define HEADER_GET_OFFSET_HIGH(q) (((q) >> 0) & 0xffff)
1114#define HEADER_GET_DATA_LENGTH(q) (((q) >> 16) & 0xffff)
1115#define HEADER_GET_EXTENDED_TCODE(q) (((q) >> 0) & 0xffff)
Kristian Høgsberg93c4cce2007-01-26 00:38:26 -05001116
Stefan Richter53dca512008-12-14 21:47:04 +01001117static void handle_local_rom(struct fw_ohci *ohci,
1118 struct fw_packet *packet, u32 csr)
Kristian Høgsberg93c4cce2007-01-26 00:38:26 -05001119{
1120 struct fw_packet response;
1121 int tcode, length, i;
1122
Kristian Høgsberga77754a2007-05-07 20:33:35 -04001123 tcode = HEADER_GET_TCODE(packet->header[0]);
Kristian Høgsberg93c4cce2007-01-26 00:38:26 -05001124 if (TCODE_IS_BLOCK_PACKET(tcode))
Kristian Høgsberga77754a2007-05-07 20:33:35 -04001125 length = HEADER_GET_DATA_LENGTH(packet->header[3]);
Kristian Høgsberg93c4cce2007-01-26 00:38:26 -05001126 else
1127 length = 4;
1128
1129 i = csr - CSR_CONFIG_ROM;
1130 if (i + length > CONFIG_ROM_SIZE) {
1131 fw_fill_response(&response, packet->header,
1132 RCODE_ADDRESS_ERROR, NULL, 0);
1133 } else if (!TCODE_IS_READ_REQUEST(tcode)) {
1134 fw_fill_response(&response, packet->header,
1135 RCODE_TYPE_ERROR, NULL, 0);
1136 } else {
1137 fw_fill_response(&response, packet->header, RCODE_COMPLETE,
1138 (void *) ohci->config_rom + i, length);
1139 }
1140
1141 fw_core_handle_response(&ohci->card, &response);
1142}
1143
Stefan Richter53dca512008-12-14 21:47:04 +01001144static void handle_local_lock(struct fw_ohci *ohci,
1145 struct fw_packet *packet, u32 csr)
Kristian Høgsberg93c4cce2007-01-26 00:38:26 -05001146{
1147 struct fw_packet response;
1148 int tcode, length, ext_tcode, sel;
1149 __be32 *payload, lock_old;
1150 u32 lock_arg, lock_data;
1151
Kristian Høgsberga77754a2007-05-07 20:33:35 -04001152 tcode = HEADER_GET_TCODE(packet->header[0]);
1153 length = HEADER_GET_DATA_LENGTH(packet->header[3]);
Kristian Høgsberg93c4cce2007-01-26 00:38:26 -05001154 payload = packet->payload;
Kristian Høgsberga77754a2007-05-07 20:33:35 -04001155 ext_tcode = HEADER_GET_EXTENDED_TCODE(packet->header[3]);
Kristian Høgsberg93c4cce2007-01-26 00:38:26 -05001156
1157 if (tcode == TCODE_LOCK_REQUEST &&
1158 ext_tcode == EXTCODE_COMPARE_SWAP && length == 8) {
1159 lock_arg = be32_to_cpu(payload[0]);
1160 lock_data = be32_to_cpu(payload[1]);
1161 } else if (tcode == TCODE_READ_QUADLET_REQUEST) {
1162 lock_arg = 0;
1163 lock_data = 0;
1164 } else {
1165 fw_fill_response(&response, packet->header,
1166 RCODE_TYPE_ERROR, NULL, 0);
1167 goto out;
1168 }
1169
1170 sel = (csr - CSR_BUS_MANAGER_ID) / 4;
1171 reg_write(ohci, OHCI1394_CSRData, lock_data);
1172 reg_write(ohci, OHCI1394_CSRCompareData, lock_arg);
1173 reg_write(ohci, OHCI1394_CSRControl, sel);
1174
1175 if (reg_read(ohci, OHCI1394_CSRControl) & 0x80000000)
1176 lock_old = cpu_to_be32(reg_read(ohci, OHCI1394_CSRData));
1177 else
1178 fw_notify("swap not done yet\n");
1179
1180 fw_fill_response(&response, packet->header,
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -04001181 RCODE_COMPLETE, &lock_old, sizeof(lock_old));
Kristian Høgsberg93c4cce2007-01-26 00:38:26 -05001182 out:
1183 fw_core_handle_response(&ohci->card, &response);
1184}
1185
Stefan Richter53dca512008-12-14 21:47:04 +01001186static void handle_local_request(struct context *ctx, struct fw_packet *packet)
Kristian Høgsberg93c4cce2007-01-26 00:38:26 -05001187{
1188 u64 offset;
1189 u32 csr;
1190
Kristian Høgsberg473d28c2007-03-07 12:12:55 -05001191 if (ctx == &ctx->ohci->at_request_ctx) {
1192 packet->ack = ACK_PENDING;
1193 packet->callback(packet, &ctx->ohci->card, packet->ack);
1194 }
Kristian Høgsberg93c4cce2007-01-26 00:38:26 -05001195
1196 offset =
1197 ((unsigned long long)
Kristian Høgsberga77754a2007-05-07 20:33:35 -04001198 HEADER_GET_OFFSET_HIGH(packet->header[1]) << 32) |
Kristian Høgsberg93c4cce2007-01-26 00:38:26 -05001199 packet->header[2];
1200 csr = offset - CSR_REGISTER_BASE;
1201
1202 /* Handle config rom reads. */
1203 if (csr >= CSR_CONFIG_ROM && csr < CSR_CONFIG_ROM_END)
1204 handle_local_rom(ctx->ohci, packet, csr);
1205 else switch (csr) {
1206 case CSR_BUS_MANAGER_ID:
1207 case CSR_BANDWIDTH_AVAILABLE:
1208 case CSR_CHANNELS_AVAILABLE_HI:
1209 case CSR_CHANNELS_AVAILABLE_LO:
1210 handle_local_lock(ctx->ohci, packet, csr);
1211 break;
1212 default:
1213 if (ctx == &ctx->ohci->at_request_ctx)
1214 fw_core_handle_request(&ctx->ohci->card, packet);
1215 else
1216 fw_core_handle_response(&ctx->ohci->card, packet);
1217 break;
1218 }
Kristian Høgsberg473d28c2007-03-07 12:12:55 -05001219
1220 if (ctx == &ctx->ohci->at_response_ctx) {
1221 packet->ack = ACK_COMPLETE;
1222 packet->callback(packet, &ctx->ohci->card, packet->ack);
1223 }
Kristian Høgsberg93c4cce2007-01-26 00:38:26 -05001224}
Kristian Høgsberge636fe22007-01-26 00:38:04 -05001225
Stefan Richter53dca512008-12-14 21:47:04 +01001226static void at_context_transmit(struct context *ctx, struct fw_packet *packet)
Kristian Høgsberged568912006-12-19 19:58:35 -05001227{
Kristian Høgsberged568912006-12-19 19:58:35 -05001228 unsigned long flags;
Stefan Richter2dbd7d72008-12-14 21:45:45 +01001229 int ret;
Kristian Høgsberged568912006-12-19 19:58:35 -05001230
1231 spin_lock_irqsave(&ctx->ohci->lock, flags);
1232
Kristian Høgsberga77754a2007-05-07 20:33:35 -04001233 if (HEADER_GET_DESTINATION(packet->header[0]) == ctx->ohci->node_id &&
Kristian Høgsberge636fe22007-01-26 00:38:04 -05001234 ctx->ohci->generation == packet->generation) {
Kristian Høgsberg93c4cce2007-01-26 00:38:26 -05001235 spin_unlock_irqrestore(&ctx->ohci->lock, flags);
1236 handle_local_request(ctx, packet);
1237 return;
Kristian Høgsberge636fe22007-01-26 00:38:04 -05001238 }
Kristian Høgsberged568912006-12-19 19:58:35 -05001239
Stefan Richter2dbd7d72008-12-14 21:45:45 +01001240 ret = at_context_queue_packet(ctx, packet);
Kristian Høgsberged568912006-12-19 19:58:35 -05001241 spin_unlock_irqrestore(&ctx->ohci->lock, flags);
1242
Stefan Richter2dbd7d72008-12-14 21:45:45 +01001243 if (ret < 0)
Kristian Høgsbergf319b6a2007-03-07 12:12:49 -05001244 packet->callback(packet, &ctx->ohci->card, packet->ack);
Jarod Wilsona186b4a2007-12-03 13:43:12 -05001245
Kristian Høgsberged568912006-12-19 19:58:35 -05001246}
1247
1248static void bus_reset_tasklet(unsigned long data)
1249{
1250 struct fw_ohci *ohci = (struct fw_ohci *)data;
Kristian Høgsberge636fe22007-01-26 00:38:04 -05001251 int self_id_count, i, j, reg;
Kristian Høgsberged568912006-12-19 19:58:35 -05001252 int generation, new_generation;
1253 unsigned long flags;
Stefan Richter4eaff7d2007-07-25 19:18:08 +02001254 void *free_rom = NULL;
1255 dma_addr_t free_rom_bus = 0;
Kristian Høgsberged568912006-12-19 19:58:35 -05001256
1257 reg = reg_read(ohci, OHCI1394_NodeID);
1258 if (!(reg & OHCI1394_NodeID_idValid)) {
Stefan Richter02ff8f82007-08-30 00:11:40 +02001259 fw_notify("node ID not valid, new bus reset in progress\n");
Kristian Høgsberged568912006-12-19 19:58:35 -05001260 return;
1261 }
Stefan Richter02ff8f82007-08-30 00:11:40 +02001262 if ((reg & OHCI1394_NodeID_nodeNumber) == 63) {
1263 fw_notify("malconfigured bus\n");
1264 return;
1265 }
1266 ohci->node_id = reg & (OHCI1394_NodeID_busNumber |
1267 OHCI1394_NodeID_nodeNumber);
Kristian Høgsberged568912006-12-19 19:58:35 -05001268
Stefan Richterc8a9a492008-03-19 21:40:32 +01001269 reg = reg_read(ohci, OHCI1394_SelfIDCount);
1270 if (reg & OHCI1394_SelfIDCount_selfIDError) {
1271 fw_notify("inconsistent self IDs\n");
1272 return;
1273 }
Kristian Høgsbergc781c062007-05-07 20:33:32 -04001274 /*
1275 * The count in the SelfIDCount register is the number of
Kristian Høgsberged568912006-12-19 19:58:35 -05001276 * bytes in the self ID receive buffer. Since we also receive
1277 * the inverted quadlets and a header quadlet, we shift one
Kristian Høgsbergc781c062007-05-07 20:33:32 -04001278 * bit extra to get the actual number of self IDs.
1279 */
Stefan Richterc8a9a492008-03-19 21:40:32 +01001280 self_id_count = (reg >> 3) & 0x3ff;
Stefan Richter016bf3d2008-03-19 22:05:02 +01001281 if (self_id_count == 0) {
1282 fw_notify("inconsistent self IDs\n");
1283 return;
1284 }
Stefan Richter11bf20a2008-03-01 02:47:15 +01001285 generation = (cond_le32_to_cpu(ohci->self_id_cpu[0]) >> 16) & 0xff;
Stefan Richteree71c2f2007-08-25 14:08:19 +02001286 rmb();
Kristian Høgsberged568912006-12-19 19:58:35 -05001287
1288 for (i = 1, j = 0; j < self_id_count; i += 2, j++) {
Stefan Richterc8a9a492008-03-19 21:40:32 +01001289 if (ohci->self_id_cpu[i] != ~ohci->self_id_cpu[i + 1]) {
1290 fw_notify("inconsistent self IDs\n");
1291 return;
1292 }
Stefan Richter11bf20a2008-03-01 02:47:15 +01001293 ohci->self_id_buffer[j] =
1294 cond_le32_to_cpu(ohci->self_id_cpu[i]);
Kristian Høgsberged568912006-12-19 19:58:35 -05001295 }
Stefan Richteree71c2f2007-08-25 14:08:19 +02001296 rmb();
Kristian Høgsberged568912006-12-19 19:58:35 -05001297
Kristian Høgsbergc781c062007-05-07 20:33:32 -04001298 /*
1299 * Check the consistency of the self IDs we just read. The
Kristian Høgsberged568912006-12-19 19:58:35 -05001300 * problem we face is that a new bus reset can start while we
1301 * read out the self IDs from the DMA buffer. If this happens,
1302 * the DMA buffer will be overwritten with new self IDs and we
1303 * will read out inconsistent data. The OHCI specification
1304 * (section 11.2) recommends a technique similar to
1305 * linux/seqlock.h, where we remember the generation of the
1306 * self IDs in the buffer before reading them out and compare
1307 * it to the current generation after reading them out. If
1308 * the two generations match we know we have a consistent set
Kristian Høgsbergc781c062007-05-07 20:33:32 -04001309 * of self IDs.
1310 */
Kristian Høgsberged568912006-12-19 19:58:35 -05001311
1312 new_generation = (reg_read(ohci, OHCI1394_SelfIDCount) >> 16) & 0xff;
1313 if (new_generation != generation) {
1314 fw_notify("recursive bus reset detected, "
1315 "discarding self ids\n");
1316 return;
1317 }
1318
1319 /* FIXME: Document how the locking works. */
1320 spin_lock_irqsave(&ohci->lock, flags);
1321
1322 ohci->generation = generation;
Kristian Høgsbergf319b6a2007-03-07 12:12:49 -05001323 context_stop(&ohci->at_request_ctx);
1324 context_stop(&ohci->at_response_ctx);
Kristian Høgsberged568912006-12-19 19:58:35 -05001325 reg_write(ohci, OHCI1394_IntEventClear, OHCI1394_busReset);
1326
Stefan Richterd34316a2008-04-12 22:31:25 +02001327 if (ohci->bus_reset_packet_quirk)
1328 ohci->request_generation = generation;
1329
Kristian Høgsbergc781c062007-05-07 20:33:32 -04001330 /*
1331 * This next bit is unrelated to the AT context stuff but we
Kristian Høgsberged568912006-12-19 19:58:35 -05001332 * have to do it under the spinlock also. If a new config rom
1333 * was set up before this reset, the old one is now no longer
1334 * in use and we can free it. Update the config rom pointers
1335 * to point to the current config rom and clear the
Kristian Høgsbergc781c062007-05-07 20:33:32 -04001336 * next_config_rom pointer so a new udpate can take place.
1337 */
Kristian Høgsberged568912006-12-19 19:58:35 -05001338
1339 if (ohci->next_config_rom != NULL) {
Kristian Høgsberg0bd243c2007-06-05 19:27:05 -04001340 if (ohci->next_config_rom != ohci->config_rom) {
1341 free_rom = ohci->config_rom;
1342 free_rom_bus = ohci->config_rom_bus;
1343 }
Kristian Høgsberged568912006-12-19 19:58:35 -05001344 ohci->config_rom = ohci->next_config_rom;
1345 ohci->config_rom_bus = ohci->next_config_rom_bus;
1346 ohci->next_config_rom = NULL;
1347
Kristian Høgsbergc781c062007-05-07 20:33:32 -04001348 /*
1349 * Restore config_rom image and manually update
Kristian Høgsberged568912006-12-19 19:58:35 -05001350 * config_rom registers. Writing the header quadlet
1351 * will indicate that the config rom is ready, so we
Kristian Høgsbergc781c062007-05-07 20:33:32 -04001352 * do that last.
1353 */
Kristian Høgsberged568912006-12-19 19:58:35 -05001354 reg_write(ohci, OHCI1394_BusOptions,
1355 be32_to_cpu(ohci->config_rom[2]));
1356 ohci->config_rom[0] = cpu_to_be32(ohci->next_header);
1357 reg_write(ohci, OHCI1394_ConfigROMhdr, ohci->next_header);
1358 }
1359
Stefan Richter080de8c2008-02-28 20:54:43 +01001360#ifdef CONFIG_FIREWIRE_OHCI_REMOTE_DMA
1361 reg_write(ohci, OHCI1394_PhyReqFilterHiSet, ~0);
1362 reg_write(ohci, OHCI1394_PhyReqFilterLoSet, ~0);
1363#endif
1364
Kristian Høgsberged568912006-12-19 19:58:35 -05001365 spin_unlock_irqrestore(&ohci->lock, flags);
1366
Stefan Richter4eaff7d2007-07-25 19:18:08 +02001367 if (free_rom)
1368 dma_free_coherent(ohci->card.device, CONFIG_ROM_SIZE,
1369 free_rom, free_rom_bus);
1370
Stefan Richter08ddb2f2008-04-11 00:51:15 +02001371 log_selfids(ohci->node_id, generation,
1372 self_id_count, ohci->self_id_buffer);
Stefan Richterad3c0fe2008-03-20 22:04:36 +01001373
Kristian Høgsberge636fe22007-01-26 00:38:04 -05001374 fw_core_handle_bus_reset(&ohci->card, ohci->node_id, generation,
Kristian Høgsberged568912006-12-19 19:58:35 -05001375 self_id_count, ohci->self_id_buffer);
1376}
1377
1378static irqreturn_t irq_handler(int irq, void *data)
1379{
1380 struct fw_ohci *ohci = data;
Kristian Høgsbergd60d7f12007-03-07 12:12:56 -05001381 u32 event, iso_event, cycle_time;
Kristian Høgsberged568912006-12-19 19:58:35 -05001382 int i;
1383
1384 event = reg_read(ohci, OHCI1394_IntEventClear);
1385
Stefan Richtera5159582007-06-09 19:31:14 +02001386 if (!event || !~event)
Kristian Høgsberged568912006-12-19 19:58:35 -05001387 return IRQ_NONE;
1388
Stefan Richtera007bb82008-04-07 22:33:35 +02001389 /* busReset must not be cleared yet, see OHCI 1.1 clause 7.2.3.2 */
1390 reg_write(ohci, OHCI1394_IntEventClear, event & ~OHCI1394_busReset);
Stefan Richterad3c0fe2008-03-20 22:04:36 +01001391 log_irqs(event);
Kristian Høgsberged568912006-12-19 19:58:35 -05001392
1393 if (event & OHCI1394_selfIDComplete)
1394 tasklet_schedule(&ohci->bus_reset_tasklet);
1395
1396 if (event & OHCI1394_RQPkt)
1397 tasklet_schedule(&ohci->ar_request_ctx.tasklet);
1398
1399 if (event & OHCI1394_RSPkt)
1400 tasklet_schedule(&ohci->ar_response_ctx.tasklet);
1401
1402 if (event & OHCI1394_reqTxComplete)
1403 tasklet_schedule(&ohci->at_request_ctx.tasklet);
1404
1405 if (event & OHCI1394_respTxComplete)
1406 tasklet_schedule(&ohci->at_response_ctx.tasklet);
1407
Kristian Høgsbergc8894752007-02-16 17:34:36 -05001408 iso_event = reg_read(ohci, OHCI1394_IsoRecvIntEventClear);
Kristian Høgsberged568912006-12-19 19:58:35 -05001409 reg_write(ohci, OHCI1394_IsoRecvIntEventClear, iso_event);
1410
1411 while (iso_event) {
1412 i = ffs(iso_event) - 1;
Kristian Høgsberg30200732007-02-16 17:34:39 -05001413 tasklet_schedule(&ohci->ir_context_list[i].context.tasklet);
Kristian Høgsberged568912006-12-19 19:58:35 -05001414 iso_event &= ~(1 << i);
1415 }
1416
Kristian Høgsbergc8894752007-02-16 17:34:36 -05001417 iso_event = reg_read(ohci, OHCI1394_IsoXmitIntEventClear);
Kristian Høgsberged568912006-12-19 19:58:35 -05001418 reg_write(ohci, OHCI1394_IsoXmitIntEventClear, iso_event);
1419
1420 while (iso_event) {
1421 i = ffs(iso_event) - 1;
Kristian Høgsberg30200732007-02-16 17:34:39 -05001422 tasklet_schedule(&ohci->it_context_list[i].context.tasklet);
Kristian Høgsberged568912006-12-19 19:58:35 -05001423 iso_event &= ~(1 << i);
1424 }
1425
Jarod Wilson75f78322008-04-03 17:18:23 -04001426 if (unlikely(event & OHCI1394_regAccessFail))
1427 fw_error("Register access failure - "
1428 "please notify linux1394-devel@lists.sf.net\n");
1429
Stefan Richtere524f6162007-08-20 21:58:30 +02001430 if (unlikely(event & OHCI1394_postedWriteErr))
1431 fw_error("PCI posted write error\n");
1432
Stefan Richterbb9f2202007-12-22 22:14:52 +01001433 if (unlikely(event & OHCI1394_cycleTooLong)) {
1434 if (printk_ratelimit())
1435 fw_notify("isochronous cycle too long\n");
1436 reg_write(ohci, OHCI1394_LinkControlSet,
1437 OHCI1394_LinkControl_cycleMaster);
1438 }
1439
Kristian Høgsbergd60d7f12007-03-07 12:12:56 -05001440 if (event & OHCI1394_cycle64Seconds) {
1441 cycle_time = reg_read(ohci, OHCI1394_IsochronousCycleTimer);
1442 if ((cycle_time & 0x80000000) == 0)
Stefan Richter3dcdc502009-06-04 21:08:43 +02001443 atomic_inc(&ohci->bus_seconds);
Kristian Høgsbergd60d7f12007-03-07 12:12:56 -05001444 }
1445
Kristian Høgsberged568912006-12-19 19:58:35 -05001446 return IRQ_HANDLED;
1447}
1448
Kristian Høgsberg2aef4692007-05-30 19:06:35 -04001449static int software_reset(struct fw_ohci *ohci)
1450{
1451 int i;
1452
1453 reg_write(ohci, OHCI1394_HCControlSet, OHCI1394_HCControl_softReset);
1454
1455 for (i = 0; i < OHCI_LOOP_COUNT; i++) {
1456 if ((reg_read(ohci, OHCI1394_HCControlSet) &
1457 OHCI1394_HCControl_softReset) == 0)
1458 return 0;
1459 msleep(1);
1460 }
1461
1462 return -EBUSY;
1463}
1464
Kristian Høgsberged568912006-12-19 19:58:35 -05001465static int ohci_enable(struct fw_card *card, u32 *config_rom, size_t length)
1466{
1467 struct fw_ohci *ohci = fw_ohci(card);
1468 struct pci_dev *dev = to_pci_dev(card->device);
Jarod Wilson02214722008-03-28 10:02:50 -04001469 u32 lps;
1470 int i;
Kristian Høgsberged568912006-12-19 19:58:35 -05001471
Kristian Høgsberg2aef4692007-05-30 19:06:35 -04001472 if (software_reset(ohci)) {
1473 fw_error("Failed to reset ohci card.\n");
1474 return -EBUSY;
1475 }
1476
1477 /*
1478 * Now enable LPS, which we need in order to start accessing
1479 * most of the registers. In fact, on some cards (ALI M5251),
1480 * accessing registers in the SClk domain without LPS enabled
1481 * will lock up the machine. Wait 50msec to make sure we have
Jarod Wilson02214722008-03-28 10:02:50 -04001482 * full link enabled. However, with some cards (well, at least
1483 * a JMicron PCIe card), we have to try again sometimes.
Kristian Høgsberg2aef4692007-05-30 19:06:35 -04001484 */
1485 reg_write(ohci, OHCI1394_HCControlSet,
1486 OHCI1394_HCControl_LPS |
1487 OHCI1394_HCControl_postedWriteEnable);
1488 flush_writes(ohci);
Jarod Wilson02214722008-03-28 10:02:50 -04001489
1490 for (lps = 0, i = 0; !lps && i < 3; i++) {
1491 msleep(50);
1492 lps = reg_read(ohci, OHCI1394_HCControlSet) &
1493 OHCI1394_HCControl_LPS;
1494 }
1495
1496 if (!lps) {
1497 fw_error("Failed to set Link Power Status\n");
1498 return -EIO;
1499 }
Kristian Høgsberg2aef4692007-05-30 19:06:35 -04001500
1501 reg_write(ohci, OHCI1394_HCControlClear,
1502 OHCI1394_HCControl_noByteSwapData);
1503
Stefan Richteraffc9c22008-06-05 20:50:53 +02001504 reg_write(ohci, OHCI1394_SelfIDBuffer, ohci->self_id_bus);
Stefan Richtere896ec42008-06-05 20:49:38 +02001505 reg_write(ohci, OHCI1394_LinkControlClear,
1506 OHCI1394_LinkControl_rcvPhyPkt);
Kristian Høgsberg2aef4692007-05-30 19:06:35 -04001507 reg_write(ohci, OHCI1394_LinkControlSet,
1508 OHCI1394_LinkControl_rcvSelfID |
1509 OHCI1394_LinkControl_cycleTimerEnable |
1510 OHCI1394_LinkControl_cycleMaster);
1511
1512 reg_write(ohci, OHCI1394_ATRetries,
1513 OHCI1394_MAX_AT_REQ_RETRIES |
1514 (OHCI1394_MAX_AT_RESP_RETRIES << 4) |
1515 (OHCI1394_MAX_PHYS_RESP_RETRIES << 8));
1516
1517 ar_context_run(&ohci->ar_request_ctx);
1518 ar_context_run(&ohci->ar_response_ctx);
1519
Kristian Høgsberg2aef4692007-05-30 19:06:35 -04001520 reg_write(ohci, OHCI1394_PhyUpperBound, 0x00010000);
1521 reg_write(ohci, OHCI1394_IntEventClear, ~0);
1522 reg_write(ohci, OHCI1394_IntMaskClear, ~0);
1523 reg_write(ohci, OHCI1394_IntMaskSet,
1524 OHCI1394_selfIDComplete |
1525 OHCI1394_RQPkt | OHCI1394_RSPkt |
1526 OHCI1394_reqTxComplete | OHCI1394_respTxComplete |
1527 OHCI1394_isochRx | OHCI1394_isochTx |
Stefan Richterbb9f2202007-12-22 22:14:52 +01001528 OHCI1394_postedWriteErr | OHCI1394_cycleTooLong |
Jarod Wilson75f78322008-04-03 17:18:23 -04001529 OHCI1394_cycle64Seconds | OHCI1394_regAccessFail |
1530 OHCI1394_masterIntEnable);
Stefan Richtera007bb82008-04-07 22:33:35 +02001531 if (param_debug & OHCI_PARAM_DEBUG_BUSRESETS)
1532 reg_write(ohci, OHCI1394_IntMaskSet, OHCI1394_busReset);
Kristian Høgsberg2aef4692007-05-30 19:06:35 -04001533
1534 /* Activate link_on bit and contender bit in our self ID packets.*/
1535 if (ohci_update_phy_reg(card, 4, 0,
1536 PHY_LINK_ACTIVE | PHY_CONTENDER) < 0)
1537 return -EIO;
1538
Kristian Høgsbergc781c062007-05-07 20:33:32 -04001539 /*
1540 * When the link is not yet enabled, the atomic config rom
Kristian Høgsberged568912006-12-19 19:58:35 -05001541 * update mechanism described below in ohci_set_config_rom()
1542 * is not active. We have to update ConfigRomHeader and
1543 * BusOptions manually, and the write to ConfigROMmap takes
1544 * effect immediately. We tie this to the enabling of the
1545 * link, so we have a valid config rom before enabling - the
1546 * OHCI requires that ConfigROMhdr and BusOptions have valid
1547 * values before enabling.
1548 *
1549 * However, when the ConfigROMmap is written, some controllers
1550 * always read back quadlets 0 and 2 from the config rom to
1551 * the ConfigRomHeader and BusOptions registers on bus reset.
1552 * They shouldn't do that in this initial case where the link
1553 * isn't enabled. This means we have to use the same
1554 * workaround here, setting the bus header to 0 and then write
1555 * the right values in the bus reset tasklet.
1556 */
1557
Kristian Høgsberg0bd243c2007-06-05 19:27:05 -04001558 if (config_rom) {
1559 ohci->next_config_rom =
1560 dma_alloc_coherent(ohci->card.device, CONFIG_ROM_SIZE,
1561 &ohci->next_config_rom_bus,
1562 GFP_KERNEL);
1563 if (ohci->next_config_rom == NULL)
1564 return -ENOMEM;
Kristian Høgsberged568912006-12-19 19:58:35 -05001565
Kristian Høgsberg0bd243c2007-06-05 19:27:05 -04001566 memset(ohci->next_config_rom, 0, CONFIG_ROM_SIZE);
1567 fw_memcpy_to_be32(ohci->next_config_rom, config_rom, length * 4);
1568 } else {
1569 /*
1570 * In the suspend case, config_rom is NULL, which
1571 * means that we just reuse the old config rom.
1572 */
1573 ohci->next_config_rom = ohci->config_rom;
1574 ohci->next_config_rom_bus = ohci->config_rom_bus;
1575 }
Kristian Høgsberged568912006-12-19 19:58:35 -05001576
Kristian Høgsberg0bd243c2007-06-05 19:27:05 -04001577 ohci->next_header = be32_to_cpu(ohci->next_config_rom[0]);
Kristian Høgsberged568912006-12-19 19:58:35 -05001578 ohci->next_config_rom[0] = 0;
1579 reg_write(ohci, OHCI1394_ConfigROMhdr, 0);
Kristian Høgsberg0bd243c2007-06-05 19:27:05 -04001580 reg_write(ohci, OHCI1394_BusOptions,
1581 be32_to_cpu(ohci->next_config_rom[2]));
Kristian Høgsberged568912006-12-19 19:58:35 -05001582 reg_write(ohci, OHCI1394_ConfigROMmap, ohci->next_config_rom_bus);
1583
1584 reg_write(ohci, OHCI1394_AsReqFilterHiSet, 0x80000000);
1585
1586 if (request_irq(dev->irq, irq_handler,
Thomas Gleixner65efffa2007-03-05 18:19:51 -08001587 IRQF_SHARED, ohci_driver_name, ohci)) {
Kristian Høgsberged568912006-12-19 19:58:35 -05001588 fw_error("Failed to allocate shared interrupt %d.\n",
1589 dev->irq);
1590 dma_free_coherent(ohci->card.device, CONFIG_ROM_SIZE,
1591 ohci->config_rom, ohci->config_rom_bus);
1592 return -EIO;
1593 }
1594
1595 reg_write(ohci, OHCI1394_HCControlSet,
1596 OHCI1394_HCControl_linkEnable |
1597 OHCI1394_HCControl_BIBimageValid);
1598 flush_writes(ohci);
1599
Kristian Høgsbergc781c062007-05-07 20:33:32 -04001600 /*
1601 * We are ready to go, initiate bus reset to finish the
1602 * initialization.
1603 */
Kristian Høgsberged568912006-12-19 19:58:35 -05001604
1605 fw_core_initiate_bus_reset(&ohci->card, 1);
1606
1607 return 0;
1608}
1609
Stefan Richter53dca512008-12-14 21:47:04 +01001610static int ohci_set_config_rom(struct fw_card *card,
1611 u32 *config_rom, size_t length)
Kristian Høgsberged568912006-12-19 19:58:35 -05001612{
1613 struct fw_ohci *ohci;
1614 unsigned long flags;
Stefan Richter2dbd7d72008-12-14 21:45:45 +01001615 int ret = -EBUSY;
Kristian Høgsberged568912006-12-19 19:58:35 -05001616 __be32 *next_config_rom;
Stefan Richterf5101d582008-03-14 00:27:49 +01001617 dma_addr_t uninitialized_var(next_config_rom_bus);
Kristian Høgsberged568912006-12-19 19:58:35 -05001618
1619 ohci = fw_ohci(card);
1620
Kristian Høgsbergc781c062007-05-07 20:33:32 -04001621 /*
1622 * When the OHCI controller is enabled, the config rom update
Kristian Høgsberged568912006-12-19 19:58:35 -05001623 * mechanism is a bit tricky, but easy enough to use. See
1624 * section 5.5.6 in the OHCI specification.
1625 *
1626 * The OHCI controller caches the new config rom address in a
1627 * shadow register (ConfigROMmapNext) and needs a bus reset
1628 * for the changes to take place. When the bus reset is
1629 * detected, the controller loads the new values for the
1630 * ConfigRomHeader and BusOptions registers from the specified
1631 * config rom and loads ConfigROMmap from the ConfigROMmapNext
1632 * shadow register. All automatically and atomically.
1633 *
1634 * Now, there's a twist to this story. The automatic load of
1635 * ConfigRomHeader and BusOptions doesn't honor the
1636 * noByteSwapData bit, so with a be32 config rom, the
1637 * controller will load be32 values in to these registers
1638 * during the atomic update, even on litte endian
1639 * architectures. The workaround we use is to put a 0 in the
1640 * header quadlet; 0 is endian agnostic and means that the
1641 * config rom isn't ready yet. In the bus reset tasklet we
1642 * then set up the real values for the two registers.
1643 *
1644 * We use ohci->lock to avoid racing with the code that sets
1645 * ohci->next_config_rom to NULL (see bus_reset_tasklet).
1646 */
1647
1648 next_config_rom =
1649 dma_alloc_coherent(ohci->card.device, CONFIG_ROM_SIZE,
1650 &next_config_rom_bus, GFP_KERNEL);
1651 if (next_config_rom == NULL)
1652 return -ENOMEM;
1653
1654 spin_lock_irqsave(&ohci->lock, flags);
1655
1656 if (ohci->next_config_rom == NULL) {
1657 ohci->next_config_rom = next_config_rom;
1658 ohci->next_config_rom_bus = next_config_rom_bus;
1659
1660 memset(ohci->next_config_rom, 0, CONFIG_ROM_SIZE);
1661 fw_memcpy_to_be32(ohci->next_config_rom, config_rom,
1662 length * 4);
1663
1664 ohci->next_header = config_rom[0];
1665 ohci->next_config_rom[0] = 0;
1666
1667 reg_write(ohci, OHCI1394_ConfigROMmap,
1668 ohci->next_config_rom_bus);
Stefan Richter2dbd7d72008-12-14 21:45:45 +01001669 ret = 0;
Kristian Høgsberged568912006-12-19 19:58:35 -05001670 }
1671
1672 spin_unlock_irqrestore(&ohci->lock, flags);
1673
Kristian Høgsbergc781c062007-05-07 20:33:32 -04001674 /*
1675 * Now initiate a bus reset to have the changes take
Kristian Høgsberged568912006-12-19 19:58:35 -05001676 * effect. We clean up the old config rom memory and DMA
1677 * mappings in the bus reset tasklet, since the OHCI
1678 * controller could need to access it before the bus reset
Kristian Høgsbergc781c062007-05-07 20:33:32 -04001679 * takes effect.
1680 */
Stefan Richter2dbd7d72008-12-14 21:45:45 +01001681 if (ret == 0)
Kristian Høgsberged568912006-12-19 19:58:35 -05001682 fw_core_initiate_bus_reset(&ohci->card, 1);
Stefan Richter4eaff7d2007-07-25 19:18:08 +02001683 else
1684 dma_free_coherent(ohci->card.device, CONFIG_ROM_SIZE,
1685 next_config_rom, next_config_rom_bus);
Kristian Høgsberged568912006-12-19 19:58:35 -05001686
Stefan Richter2dbd7d72008-12-14 21:45:45 +01001687 return ret;
Kristian Høgsberged568912006-12-19 19:58:35 -05001688}
1689
1690static void ohci_send_request(struct fw_card *card, struct fw_packet *packet)
1691{
1692 struct fw_ohci *ohci = fw_ohci(card);
1693
1694 at_context_transmit(&ohci->at_request_ctx, packet);
1695}
1696
1697static void ohci_send_response(struct fw_card *card, struct fw_packet *packet)
1698{
1699 struct fw_ohci *ohci = fw_ohci(card);
1700
1701 at_context_transmit(&ohci->at_response_ctx, packet);
1702}
1703
Kristian Høgsberg730c32f2007-02-06 14:49:32 -05001704static int ohci_cancel_packet(struct fw_card *card, struct fw_packet *packet)
1705{
1706 struct fw_ohci *ohci = fw_ohci(card);
Kristian Høgsbergf319b6a2007-03-07 12:12:49 -05001707 struct context *ctx = &ohci->at_request_ctx;
1708 struct driver_data *driver_data = packet->driver_data;
Stefan Richter2dbd7d72008-12-14 21:45:45 +01001709 int ret = -ENOENT;
Kristian Høgsberg730c32f2007-02-06 14:49:32 -05001710
Kristian Høgsbergf319b6a2007-03-07 12:12:49 -05001711 tasklet_disable(&ctx->tasklet);
Kristian Høgsberg730c32f2007-02-06 14:49:32 -05001712
Kristian Høgsbergf319b6a2007-03-07 12:12:49 -05001713 if (packet->ack != 0)
1714 goto out;
Kristian Høgsberg730c32f2007-02-06 14:49:32 -05001715
Stefan Richter1d1dc5e2008-12-10 00:20:38 +01001716 if (packet->payload_bus)
1717 dma_unmap_single(ohci->card.device, packet->payload_bus,
1718 packet->payload_length, DMA_TO_DEVICE);
1719
Stefan Richterad3c0fe2008-03-20 22:04:36 +01001720 log_ar_at_event('T', packet->speed, packet->header, 0x20);
Kristian Høgsbergf319b6a2007-03-07 12:12:49 -05001721 driver_data->packet = NULL;
1722 packet->ack = RCODE_CANCELLED;
1723 packet->callback(packet, &ohci->card, packet->ack);
Stefan Richter2dbd7d72008-12-14 21:45:45 +01001724 ret = 0;
Kristian Høgsbergf319b6a2007-03-07 12:12:49 -05001725 out:
1726 tasklet_enable(&ctx->tasklet);
Kristian Høgsberg730c32f2007-02-06 14:49:32 -05001727
Stefan Richter2dbd7d72008-12-14 21:45:45 +01001728 return ret;
Kristian Høgsberg730c32f2007-02-06 14:49:32 -05001729}
1730
Stefan Richter53dca512008-12-14 21:47:04 +01001731static int ohci_enable_phys_dma(struct fw_card *card,
1732 int node_id, int generation)
Kristian Høgsberged568912006-12-19 19:58:35 -05001733{
Stefan Richter080de8c2008-02-28 20:54:43 +01001734#ifdef CONFIG_FIREWIRE_OHCI_REMOTE_DMA
1735 return 0;
1736#else
Kristian Høgsberged568912006-12-19 19:58:35 -05001737 struct fw_ohci *ohci = fw_ohci(card);
1738 unsigned long flags;
Stefan Richter2dbd7d72008-12-14 21:45:45 +01001739 int n, ret = 0;
Kristian Høgsberged568912006-12-19 19:58:35 -05001740
Kristian Høgsbergc781c062007-05-07 20:33:32 -04001741 /*
1742 * FIXME: Make sure this bitmask is cleared when we clear the busReset
1743 * interrupt bit. Clear physReqResourceAllBuses on bus reset.
1744 */
Kristian Høgsberged568912006-12-19 19:58:35 -05001745
1746 spin_lock_irqsave(&ohci->lock, flags);
1747
1748 if (ohci->generation != generation) {
Stefan Richter2dbd7d72008-12-14 21:45:45 +01001749 ret = -ESTALE;
Kristian Høgsberged568912006-12-19 19:58:35 -05001750 goto out;
1751 }
1752
Kristian Høgsbergc781c062007-05-07 20:33:32 -04001753 /*
1754 * Note, if the node ID contains a non-local bus ID, physical DMA is
1755 * enabled for _all_ nodes on remote buses.
1756 */
Stefan Richter907293d2007-01-23 21:11:43 +01001757
1758 n = (node_id & 0xffc0) == LOCAL_BUS ? node_id & 0x3f : 63;
1759 if (n < 32)
1760 reg_write(ohci, OHCI1394_PhyReqFilterLoSet, 1 << n);
1761 else
1762 reg_write(ohci, OHCI1394_PhyReqFilterHiSet, 1 << (n - 32));
1763
Kristian Høgsberged568912006-12-19 19:58:35 -05001764 flush_writes(ohci);
Kristian Høgsberged568912006-12-19 19:58:35 -05001765 out:
Stefan Richter6cad95f2007-01-21 20:46:45 +01001766 spin_unlock_irqrestore(&ohci->lock, flags);
Stefan Richter2dbd7d72008-12-14 21:45:45 +01001767
1768 return ret;
Stefan Richter080de8c2008-02-28 20:54:43 +01001769#endif /* CONFIG_FIREWIRE_OHCI_REMOTE_DMA */
Kristian Høgsberged568912006-12-19 19:58:35 -05001770}
Stefan Richter373b2ed2007-03-04 14:45:18 +01001771
Stefan Richter53dca512008-12-14 21:47:04 +01001772static u64 ohci_get_bus_time(struct fw_card *card)
Kristian Høgsbergd60d7f12007-03-07 12:12:56 -05001773{
1774 struct fw_ohci *ohci = fw_ohci(card);
1775 u32 cycle_time;
1776 u64 bus_time;
1777
1778 cycle_time = reg_read(ohci, OHCI1394_IsochronousCycleTimer);
Stefan Richter3dcdc502009-06-04 21:08:43 +02001779 bus_time = ((u64)atomic_read(&ohci->bus_seconds) << 32) | cycle_time;
Kristian Høgsbergd60d7f12007-03-07 12:12:56 -05001780
1781 return bus_time;
1782}
1783
David Moore1aa292b2008-07-22 23:23:40 -07001784static void copy_iso_headers(struct iso_context *ctx, void *p)
1785{
1786 int i = ctx->header_length;
1787
1788 if (i + ctx->base.header_size > PAGE_SIZE)
1789 return;
1790
1791 /*
1792 * The iso header is byteswapped to little endian by
1793 * the controller, but the remaining header quadlets
1794 * are big endian. We want to present all the headers
1795 * as big endian, so we have to swap the first quadlet.
1796 */
1797 if (ctx->base.header_size > 0)
1798 *(u32 *) (ctx->header + i) = __swab32(*(u32 *) (p + 4));
1799 if (ctx->base.header_size > 4)
1800 *(u32 *) (ctx->header + i + 4) = __swab32(*(u32 *) p);
1801 if (ctx->base.header_size > 8)
1802 memcpy(ctx->header + i + 8, p + 8, ctx->base.header_size - 8);
1803 ctx->header_length += ctx->base.header_size;
1804}
1805
Kristian Høgsbergd2746dc2007-02-16 17:34:46 -05001806static int handle_ir_dualbuffer_packet(struct context *context,
1807 struct descriptor *d,
1808 struct descriptor *last)
Kristian Høgsberged568912006-12-19 19:58:35 -05001809{
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -05001810 struct iso_context *ctx =
1811 container_of(context, struct iso_context, context);
1812 struct db_descriptor *db = (struct db_descriptor *) d;
Kristian Høgsbergc70dc782007-03-14 17:34:53 -04001813 __le32 *ir_header;
Kristian Høgsberg9b32d5f2007-02-16 17:34:44 -05001814 size_t header_length;
Kristian Høgsbergc70dc782007-03-14 17:34:53 -04001815 void *p, *end;
Kristian Høgsbergd2746dc2007-02-16 17:34:46 -05001816
Stefan Richterefbf3902008-02-23 12:24:57 +01001817 if (db->first_res_count != 0 && db->second_res_count != 0) {
David Moore0642b652007-12-19 03:09:18 -05001818 if (ctx->excess_bytes <= le16_to_cpu(db->second_req_count)) {
1819 /* This descriptor isn't done yet, stop iteration. */
1820 return 0;
1821 }
1822 ctx->excess_bytes -= le16_to_cpu(db->second_req_count);
1823 }
Kristian Høgsberged568912006-12-19 19:58:35 -05001824
Kristian Høgsbergc70dc782007-03-14 17:34:53 -04001825 header_length = le16_to_cpu(db->first_req_count) -
1826 le16_to_cpu(db->first_res_count);
1827
Kristian Høgsbergc70dc782007-03-14 17:34:53 -04001828 p = db + 1;
1829 end = p + header_length;
David Moore1aa292b2008-07-22 23:23:40 -07001830 while (p < end) {
1831 copy_iso_headers(ctx, p);
David Moore0642b652007-12-19 03:09:18 -05001832 ctx->excess_bytes +=
Stefan Richterefbf3902008-02-23 12:24:57 +01001833 (le32_to_cpu(*(__le32 *)(p + 4)) >> 16) & 0xffff;
David Moore1aa292b2008-07-22 23:23:40 -07001834 p += max(ctx->base.header_size, (size_t)8);
Kristian Høgsbergc70dc782007-03-14 17:34:53 -04001835 }
Kristian Høgsberg9b32d5f2007-02-16 17:34:44 -05001836
David Moore0642b652007-12-19 03:09:18 -05001837 ctx->excess_bytes -= le16_to_cpu(db->second_req_count) -
1838 le16_to_cpu(db->second_res_count);
1839
Kristian Høgsberga77754a2007-05-07 20:33:35 -04001840 if (le16_to_cpu(db->control) & DESCRIPTOR_IRQ_ALWAYS) {
Kristian Høgsbergc70dc782007-03-14 17:34:53 -04001841 ir_header = (__le32 *) (db + 1);
1842 ctx->base.callback(&ctx->base,
1843 le32_to_cpu(ir_header[0]) & 0xffff,
Kristian Høgsberg9b32d5f2007-02-16 17:34:44 -05001844 ctx->header_length, ctx->header,
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -05001845 ctx->base.callback_data);
Kristian Høgsberg9b32d5f2007-02-16 17:34:44 -05001846 ctx->header_length = 0;
1847 }
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -05001848
1849 return 1;
Kristian Høgsberged568912006-12-19 19:58:35 -05001850}
1851
Jarod Wilsona186b4a2007-12-03 13:43:12 -05001852static int handle_ir_packet_per_buffer(struct context *context,
1853 struct descriptor *d,
1854 struct descriptor *last)
1855{
1856 struct iso_context *ctx =
1857 container_of(context, struct iso_context, context);
David Moorebcee8932007-12-19 15:26:38 -05001858 struct descriptor *pd;
Jarod Wilsona186b4a2007-12-03 13:43:12 -05001859 __le32 *ir_header;
David Moorebcee8932007-12-19 15:26:38 -05001860 void *p;
Jarod Wilsona186b4a2007-12-03 13:43:12 -05001861
David Moorebcee8932007-12-19 15:26:38 -05001862 for (pd = d; pd <= last; pd++) {
1863 if (pd->transfer_status)
1864 break;
1865 }
1866 if (pd > last)
Jarod Wilsona186b4a2007-12-03 13:43:12 -05001867 /* Descriptor(s) not done yet, stop iteration */
1868 return 0;
1869
David Moore1aa292b2008-07-22 23:23:40 -07001870 p = last + 1;
1871 copy_iso_headers(ctx, p);
Jarod Wilsona186b4a2007-12-03 13:43:12 -05001872
David Moorebcee8932007-12-19 15:26:38 -05001873 if (le16_to_cpu(last->control) & DESCRIPTOR_IRQ_ALWAYS) {
1874 ir_header = (__le32 *) p;
Jarod Wilsona186b4a2007-12-03 13:43:12 -05001875 ctx->base.callback(&ctx->base,
1876 le32_to_cpu(ir_header[0]) & 0xffff,
1877 ctx->header_length, ctx->header,
1878 ctx->base.callback_data);
1879 ctx->header_length = 0;
1880 }
1881
Jarod Wilsona186b4a2007-12-03 13:43:12 -05001882 return 1;
1883}
1884
Kristian Høgsberg30200732007-02-16 17:34:39 -05001885static int handle_it_packet(struct context *context,
1886 struct descriptor *d,
1887 struct descriptor *last)
Kristian Høgsberged568912006-12-19 19:58:35 -05001888{
Kristian Høgsberg30200732007-02-16 17:34:39 -05001889 struct iso_context *ctx =
1890 container_of(context, struct iso_context, context);
Stefan Richter373b2ed2007-03-04 14:45:18 +01001891
Kristian Høgsberg30200732007-02-16 17:34:39 -05001892 if (last->transfer_status == 0)
1893 /* This descriptor isn't done yet, stop iteration. */
1894 return 0;
Kristian Høgsberged568912006-12-19 19:58:35 -05001895
Kristian Høgsberga77754a2007-05-07 20:33:35 -04001896 if (le16_to_cpu(last->control) & DESCRIPTOR_IRQ_ALWAYS)
Kristian Høgsberg9b32d5f2007-02-16 17:34:44 -05001897 ctx->base.callback(&ctx->base, le16_to_cpu(last->res_count),
1898 0, NULL, ctx->base.callback_data);
Kristian Høgsberged568912006-12-19 19:58:35 -05001899
Kristian Høgsberg30200732007-02-16 17:34:39 -05001900 return 1;
Kristian Høgsberged568912006-12-19 19:58:35 -05001901}
1902
Stefan Richter53dca512008-12-14 21:47:04 +01001903static struct fw_iso_context *ohci_allocate_iso_context(struct fw_card *card,
Stefan Richter4817ed22008-12-21 16:39:46 +01001904 int type, int channel, size_t header_size)
Kristian Høgsberged568912006-12-19 19:58:35 -05001905{
1906 struct fw_ohci *ohci = fw_ohci(card);
1907 struct iso_context *ctx, *list;
Kristian Høgsberg30200732007-02-16 17:34:39 -05001908 descriptor_callback_t callback;
Stefan Richter4817ed22008-12-21 16:39:46 +01001909 u64 *channels, dont_care = ~0ULL;
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -05001910 u32 *mask, regs;
Kristian Høgsberged568912006-12-19 19:58:35 -05001911 unsigned long flags;
Stefan Richter2dbd7d72008-12-14 21:45:45 +01001912 int index, ret = -ENOMEM;
Kristian Høgsberged568912006-12-19 19:58:35 -05001913
1914 if (type == FW_ISO_CONTEXT_TRANSMIT) {
Stefan Richter4817ed22008-12-21 16:39:46 +01001915 channels = &dont_care;
Kristian Høgsberged568912006-12-19 19:58:35 -05001916 mask = &ohci->it_context_mask;
1917 list = ohci->it_context_list;
Kristian Høgsberg30200732007-02-16 17:34:39 -05001918 callback = handle_it_packet;
Kristian Høgsberged568912006-12-19 19:58:35 -05001919 } else {
Stefan Richter4817ed22008-12-21 16:39:46 +01001920 channels = &ohci->ir_context_channels;
Stefan Richter373b2ed2007-03-04 14:45:18 +01001921 mask = &ohci->ir_context_mask;
1922 list = ohci->ir_context_list;
Stefan Richter95984f62008-07-22 18:41:10 +02001923 if (ohci->use_dualbuffer)
Jarod Wilsona186b4a2007-12-03 13:43:12 -05001924 callback = handle_ir_dualbuffer_packet;
1925 else
1926 callback = handle_ir_packet_per_buffer;
Kristian Høgsberged568912006-12-19 19:58:35 -05001927 }
1928
1929 spin_lock_irqsave(&ohci->lock, flags);
Stefan Richter4817ed22008-12-21 16:39:46 +01001930 index = *channels & 1ULL << channel ? ffs(*mask) - 1 : -1;
1931 if (index >= 0) {
1932 *channels &= ~(1ULL << channel);
Kristian Høgsberged568912006-12-19 19:58:35 -05001933 *mask &= ~(1 << index);
Stefan Richter4817ed22008-12-21 16:39:46 +01001934 }
Kristian Høgsberged568912006-12-19 19:58:35 -05001935 spin_unlock_irqrestore(&ohci->lock, flags);
1936
1937 if (index < 0)
1938 return ERR_PTR(-EBUSY);
1939
Stefan Richter373b2ed2007-03-04 14:45:18 +01001940 if (type == FW_ISO_CONTEXT_TRANSMIT)
1941 regs = OHCI1394_IsoXmitContextBase(index);
1942 else
1943 regs = OHCI1394_IsoRcvContextBase(index);
1944
Kristian Høgsberged568912006-12-19 19:58:35 -05001945 ctx = &list[index];
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -04001946 memset(ctx, 0, sizeof(*ctx));
Kristian Høgsberg9b32d5f2007-02-16 17:34:44 -05001947 ctx->header_length = 0;
1948 ctx->header = (void *) __get_free_page(GFP_KERNEL);
1949 if (ctx->header == NULL)
1950 goto out;
1951
Stefan Richter2dbd7d72008-12-14 21:45:45 +01001952 ret = context_init(&ctx->context, ohci, regs, callback);
1953 if (ret < 0)
Kristian Høgsberg9b32d5f2007-02-16 17:34:44 -05001954 goto out_with_header;
Kristian Høgsberged568912006-12-19 19:58:35 -05001955
1956 return &ctx->base;
Kristian Høgsberg9b32d5f2007-02-16 17:34:44 -05001957
1958 out_with_header:
1959 free_page((unsigned long)ctx->header);
1960 out:
1961 spin_lock_irqsave(&ohci->lock, flags);
1962 *mask |= 1 << index;
1963 spin_unlock_irqrestore(&ohci->lock, flags);
1964
Stefan Richter2dbd7d72008-12-14 21:45:45 +01001965 return ERR_PTR(ret);
Kristian Høgsberged568912006-12-19 19:58:35 -05001966}
1967
Kristian Høgsbergeb0306e2007-03-14 17:34:54 -04001968static int ohci_start_iso(struct fw_iso_context *base,
1969 s32 cycle, u32 sync, u32 tags)
Kristian Høgsberged568912006-12-19 19:58:35 -05001970{
Stefan Richter373b2ed2007-03-04 14:45:18 +01001971 struct iso_context *ctx = container_of(base, struct iso_context, base);
Kristian Høgsberg30200732007-02-16 17:34:39 -05001972 struct fw_ohci *ohci = ctx->context.ohci;
Kristian Høgsberg8a2f7d92007-03-28 14:26:10 -04001973 u32 control, match;
Kristian Høgsberged568912006-12-19 19:58:35 -05001974 int index;
1975
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -05001976 if (ctx->base.type == FW_ISO_CONTEXT_TRANSMIT) {
1977 index = ctx - ohci->it_context_list;
Kristian Høgsberg8a2f7d92007-03-28 14:26:10 -04001978 match = 0;
1979 if (cycle >= 0)
1980 match = IT_CONTEXT_CYCLE_MATCH_ENABLE |
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -05001981 (cycle & 0x7fff) << 16;
Kristian Høgsberg21efb3c2007-02-16 17:34:50 -05001982
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -05001983 reg_write(ohci, OHCI1394_IsoXmitIntEventClear, 1 << index);
1984 reg_write(ohci, OHCI1394_IsoXmitIntMaskSet, 1 << index);
Kristian Høgsberg8a2f7d92007-03-28 14:26:10 -04001985 context_run(&ctx->context, match);
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -05001986 } else {
1987 index = ctx - ohci->ir_context_list;
Jarod Wilsona186b4a2007-12-03 13:43:12 -05001988 control = IR_CONTEXT_ISOCH_HEADER;
Stefan Richter95984f62008-07-22 18:41:10 +02001989 if (ohci->use_dualbuffer)
Jarod Wilsona186b4a2007-12-03 13:43:12 -05001990 control |= IR_CONTEXT_DUAL_BUFFER_MODE;
Kristian Høgsberg8a2f7d92007-03-28 14:26:10 -04001991 match = (tags << 28) | (sync << 8) | ctx->base.channel;
1992 if (cycle >= 0) {
1993 match |= (cycle & 0x07fff) << 12;
1994 control |= IR_CONTEXT_CYCLE_MATCH_ENABLE;
1995 }
Kristian Høgsberged568912006-12-19 19:58:35 -05001996
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -05001997 reg_write(ohci, OHCI1394_IsoRecvIntEventClear, 1 << index);
1998 reg_write(ohci, OHCI1394_IsoRecvIntMaskSet, 1 << index);
Kristian Høgsberga77754a2007-05-07 20:33:35 -04001999 reg_write(ohci, CONTEXT_MATCH(ctx->context.regs), match);
Kristian Høgsberg8a2f7d92007-03-28 14:26:10 -04002000 context_run(&ctx->context, control);
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -05002001 }
Kristian Høgsberged568912006-12-19 19:58:35 -05002002
2003 return 0;
2004}
2005
Kristian Høgsbergb8295662007-02-16 17:34:42 -05002006static int ohci_stop_iso(struct fw_iso_context *base)
2007{
2008 struct fw_ohci *ohci = fw_ohci(base->card);
Stefan Richter373b2ed2007-03-04 14:45:18 +01002009 struct iso_context *ctx = container_of(base, struct iso_context, base);
Kristian Høgsbergb8295662007-02-16 17:34:42 -05002010 int index;
2011
2012 if (ctx->base.type == FW_ISO_CONTEXT_TRANSMIT) {
2013 index = ctx - ohci->it_context_list;
2014 reg_write(ohci, OHCI1394_IsoXmitIntMaskClear, 1 << index);
2015 } else {
2016 index = ctx - ohci->ir_context_list;
2017 reg_write(ohci, OHCI1394_IsoRecvIntMaskClear, 1 << index);
2018 }
2019 flush_writes(ohci);
2020 context_stop(&ctx->context);
2021
2022 return 0;
2023}
2024
Kristian Høgsberged568912006-12-19 19:58:35 -05002025static void ohci_free_iso_context(struct fw_iso_context *base)
2026{
2027 struct fw_ohci *ohci = fw_ohci(base->card);
Stefan Richter373b2ed2007-03-04 14:45:18 +01002028 struct iso_context *ctx = container_of(base, struct iso_context, base);
Kristian Høgsberged568912006-12-19 19:58:35 -05002029 unsigned long flags;
2030 int index;
2031
Kristian Høgsbergb8295662007-02-16 17:34:42 -05002032 ohci_stop_iso(base);
2033 context_release(&ctx->context);
Kristian Høgsberg9b32d5f2007-02-16 17:34:44 -05002034 free_page((unsigned long)ctx->header);
Kristian Høgsbergb8295662007-02-16 17:34:42 -05002035
Kristian Høgsberged568912006-12-19 19:58:35 -05002036 spin_lock_irqsave(&ohci->lock, flags);
2037
2038 if (ctx->base.type == FW_ISO_CONTEXT_TRANSMIT) {
2039 index = ctx - ohci->it_context_list;
Kristian Høgsberged568912006-12-19 19:58:35 -05002040 ohci->it_context_mask |= 1 << index;
2041 } else {
2042 index = ctx - ohci->ir_context_list;
Kristian Høgsberged568912006-12-19 19:58:35 -05002043 ohci->ir_context_mask |= 1 << index;
Stefan Richter4817ed22008-12-21 16:39:46 +01002044 ohci->ir_context_channels |= 1ULL << base->channel;
Kristian Høgsberged568912006-12-19 19:58:35 -05002045 }
Kristian Høgsberged568912006-12-19 19:58:35 -05002046
2047 spin_unlock_irqrestore(&ohci->lock, flags);
2048}
2049
Stefan Richter53dca512008-12-14 21:47:04 +01002050static int ohci_queue_iso_transmit(struct fw_iso_context *base,
2051 struct fw_iso_packet *packet,
2052 struct fw_iso_buffer *buffer,
2053 unsigned long payload)
Kristian Høgsberged568912006-12-19 19:58:35 -05002054{
Stefan Richter373b2ed2007-03-04 14:45:18 +01002055 struct iso_context *ctx = container_of(base, struct iso_context, base);
Kristian Høgsberg30200732007-02-16 17:34:39 -05002056 struct descriptor *d, *last, *pd;
Kristian Høgsberged568912006-12-19 19:58:35 -05002057 struct fw_iso_packet *p;
2058 __le32 *header;
Kristian Høgsberg9aad8122007-02-16 17:34:38 -05002059 dma_addr_t d_bus, page_bus;
Kristian Høgsberged568912006-12-19 19:58:35 -05002060 u32 z, header_z, payload_z, irq;
2061 u32 payload_index, payload_end_index, next_page_index;
Kristian Høgsberg30200732007-02-16 17:34:39 -05002062 int page, end_page, i, length, offset;
Kristian Høgsberged568912006-12-19 19:58:35 -05002063
Kristian Høgsbergc781c062007-05-07 20:33:32 -04002064 /*
2065 * FIXME: Cycle lost behavior should be configurable: lose
2066 * packet, retransmit or terminate..
2067 */
Kristian Høgsberged568912006-12-19 19:58:35 -05002068
2069 p = packet;
Kristian Høgsberg9aad8122007-02-16 17:34:38 -05002070 payload_index = payload;
Kristian Høgsberged568912006-12-19 19:58:35 -05002071
2072 if (p->skip)
2073 z = 1;
2074 else
2075 z = 2;
2076 if (p->header_length > 0)
2077 z++;
2078
2079 /* Determine the first page the payload isn't contained in. */
2080 end_page = PAGE_ALIGN(payload_index + p->payload_length) >> PAGE_SHIFT;
2081 if (p->payload_length > 0)
2082 payload_z = end_page - (payload_index >> PAGE_SHIFT);
2083 else
2084 payload_z = 0;
2085
2086 z += payload_z;
2087
2088 /* Get header size in number of descriptors. */
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -04002089 header_z = DIV_ROUND_UP(p->header_length, sizeof(*d));
Kristian Høgsberged568912006-12-19 19:58:35 -05002090
Kristian Høgsberg30200732007-02-16 17:34:39 -05002091 d = context_get_descriptors(&ctx->context, z + header_z, &d_bus);
2092 if (d == NULL)
2093 return -ENOMEM;
Kristian Høgsberged568912006-12-19 19:58:35 -05002094
2095 if (!p->skip) {
Kristian Høgsberga77754a2007-05-07 20:33:35 -04002096 d[0].control = cpu_to_le16(DESCRIPTOR_KEY_IMMEDIATE);
Kristian Høgsberged568912006-12-19 19:58:35 -05002097 d[0].req_count = cpu_to_le16(8);
2098
2099 header = (__le32 *) &d[1];
Kristian Høgsberga77754a2007-05-07 20:33:35 -04002100 header[0] = cpu_to_le32(IT_HEADER_SY(p->sy) |
2101 IT_HEADER_TAG(p->tag) |
2102 IT_HEADER_TCODE(TCODE_STREAM_DATA) |
2103 IT_HEADER_CHANNEL(ctx->base.channel) |
2104 IT_HEADER_SPEED(ctx->base.speed));
Kristian Høgsberged568912006-12-19 19:58:35 -05002105 header[1] =
Kristian Høgsberga77754a2007-05-07 20:33:35 -04002106 cpu_to_le32(IT_HEADER_DATA_LENGTH(p->header_length +
Kristian Høgsberged568912006-12-19 19:58:35 -05002107 p->payload_length));
2108 }
2109
2110 if (p->header_length > 0) {
2111 d[2].req_count = cpu_to_le16(p->header_length);
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -04002112 d[2].data_address = cpu_to_le32(d_bus + z * sizeof(*d));
Kristian Høgsberged568912006-12-19 19:58:35 -05002113 memcpy(&d[z], p->header, p->header_length);
2114 }
2115
2116 pd = d + z - payload_z;
2117 payload_end_index = payload_index + p->payload_length;
2118 for (i = 0; i < payload_z; i++) {
2119 page = payload_index >> PAGE_SHIFT;
2120 offset = payload_index & ~PAGE_MASK;
2121 next_page_index = (page + 1) << PAGE_SHIFT;
2122 length =
2123 min(next_page_index, payload_end_index) - payload_index;
2124 pd[i].req_count = cpu_to_le16(length);
Kristian Høgsberg9aad8122007-02-16 17:34:38 -05002125
2126 page_bus = page_private(buffer->pages[page]);
2127 pd[i].data_address = cpu_to_le32(page_bus + offset);
Kristian Høgsberged568912006-12-19 19:58:35 -05002128
2129 payload_index += length;
2130 }
2131
Kristian Høgsberged568912006-12-19 19:58:35 -05002132 if (p->interrupt)
Kristian Høgsberga77754a2007-05-07 20:33:35 -04002133 irq = DESCRIPTOR_IRQ_ALWAYS;
Kristian Høgsberged568912006-12-19 19:58:35 -05002134 else
Kristian Høgsberga77754a2007-05-07 20:33:35 -04002135 irq = DESCRIPTOR_NO_IRQ;
Kristian Høgsberged568912006-12-19 19:58:35 -05002136
Kristian Høgsberg30200732007-02-16 17:34:39 -05002137 last = z == 2 ? d : d + z - 1;
Kristian Høgsberga77754a2007-05-07 20:33:35 -04002138 last->control |= cpu_to_le16(DESCRIPTOR_OUTPUT_LAST |
2139 DESCRIPTOR_STATUS |
2140 DESCRIPTOR_BRANCH_ALWAYS |
Kristian Høgsbergcbb59da2007-02-16 17:34:35 -05002141 irq);
Kristian Høgsberged568912006-12-19 19:58:35 -05002142
Kristian Høgsberg30200732007-02-16 17:34:39 -05002143 context_append(&ctx->context, d, z, header_z);
Kristian Høgsberged568912006-12-19 19:58:35 -05002144
2145 return 0;
2146}
Stefan Richter373b2ed2007-03-04 14:45:18 +01002147
Stefan Richter53dca512008-12-14 21:47:04 +01002148static int ohci_queue_iso_receive_dualbuffer(struct fw_iso_context *base,
2149 struct fw_iso_packet *packet,
2150 struct fw_iso_buffer *buffer,
2151 unsigned long payload)
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -05002152{
2153 struct iso_context *ctx = container_of(base, struct iso_context, base);
2154 struct db_descriptor *db = NULL;
2155 struct descriptor *d;
2156 struct fw_iso_packet *p;
2157 dma_addr_t d_bus, page_bus;
2158 u32 z, header_z, length, rest;
Kristian Høgsbergc70dc782007-03-14 17:34:53 -04002159 int page, offset, packet_count, header_size;
Stefan Richter373b2ed2007-03-04 14:45:18 +01002160
Kristian Høgsbergc781c062007-05-07 20:33:32 -04002161 /*
2162 * FIXME: Cycle lost behavior should be configurable: lose
2163 * packet, retransmit or terminate..
2164 */
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -05002165
2166 p = packet;
2167 z = 2;
2168
Kristian Høgsbergc781c062007-05-07 20:33:32 -04002169 /*
David Moore1aa292b2008-07-22 23:23:40 -07002170 * The OHCI controller puts the isochronous header and trailer in the
2171 * buffer, so we need at least 8 bytes.
Kristian Høgsbergc781c062007-05-07 20:33:32 -04002172 */
Kristian Høgsbergc70dc782007-03-14 17:34:53 -04002173 packet_count = p->header_length / ctx->base.header_size;
David Moore1aa292b2008-07-22 23:23:40 -07002174 header_size = packet_count * max(ctx->base.header_size, (size_t)8);
Kristian Høgsbergc70dc782007-03-14 17:34:53 -04002175
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -05002176 /* Get header size in number of descriptors. */
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -04002177 header_z = DIV_ROUND_UP(header_size, sizeof(*d));
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -05002178 page = payload >> PAGE_SHIFT;
2179 offset = payload & ~PAGE_MASK;
2180 rest = p->payload_length;
2181
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -05002182 /* FIXME: make packet-per-buffer/dual-buffer a context option */
2183 while (rest > 0) {
2184 d = context_get_descriptors(&ctx->context,
2185 z + header_z, &d_bus);
2186 if (d == NULL)
2187 return -ENOMEM;
2188
2189 db = (struct db_descriptor *) d;
Kristian Høgsberga77754a2007-05-07 20:33:35 -04002190 db->control = cpu_to_le16(DESCRIPTOR_STATUS |
2191 DESCRIPTOR_BRANCH_ALWAYS);
David Moore1aa292b2008-07-22 23:23:40 -07002192 db->first_size =
2193 cpu_to_le16(max(ctx->base.header_size, (size_t)8));
David Moore0642b652007-12-19 03:09:18 -05002194 if (p->skip && rest == p->payload_length) {
2195 db->control |= cpu_to_le16(DESCRIPTOR_WAIT);
2196 db->first_req_count = db->first_size;
2197 } else {
2198 db->first_req_count = cpu_to_le16(header_size);
2199 }
Kristian Høgsberg1e1d1962007-02-16 17:34:45 -05002200 db->first_res_count = db->first_req_count;
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -04002201 db->first_buffer = cpu_to_le32(d_bus + sizeof(*db));
Stefan Richter373b2ed2007-03-04 14:45:18 +01002202
David Moore0642b652007-12-19 03:09:18 -05002203 if (p->skip && rest == p->payload_length)
2204 length = 4;
2205 else if (offset + rest < PAGE_SIZE)
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -05002206 length = rest;
2207 else
2208 length = PAGE_SIZE - offset;
2209
Kristian Høgsberg1e1d1962007-02-16 17:34:45 -05002210 db->second_req_count = cpu_to_le16(length);
2211 db->second_res_count = db->second_req_count;
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -05002212 page_bus = page_private(buffer->pages[page]);
2213 db->second_buffer = cpu_to_le32(page_bus + offset);
2214
Kristian Høgsbergcb2d2cd2007-02-16 17:34:47 -05002215 if (p->interrupt && length == rest)
Kristian Høgsberga77754a2007-05-07 20:33:35 -04002216 db->control |= cpu_to_le16(DESCRIPTOR_IRQ_ALWAYS);
Kristian Høgsbergcb2d2cd2007-02-16 17:34:47 -05002217
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -05002218 context_append(&ctx->context, d, z, header_z);
2219 offset = (offset + length) & ~PAGE_MASK;
2220 rest -= length;
David Moore0642b652007-12-19 03:09:18 -05002221 if (offset == 0)
2222 page++;
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -05002223 }
2224
Kristian Høgsbergd2746dc2007-02-16 17:34:46 -05002225 return 0;
2226}
Kristian Høgsberg21efb3c2007-02-16 17:34:50 -05002227
Stefan Richter53dca512008-12-14 21:47:04 +01002228static int ohci_queue_iso_receive_packet_per_buffer(struct fw_iso_context *base,
2229 struct fw_iso_packet *packet,
2230 struct fw_iso_buffer *buffer,
2231 unsigned long payload)
Jarod Wilsona186b4a2007-12-03 13:43:12 -05002232{
2233 struct iso_context *ctx = container_of(base, struct iso_context, base);
2234 struct descriptor *d = NULL, *pd = NULL;
David Moorebcee8932007-12-19 15:26:38 -05002235 struct fw_iso_packet *p = packet;
Jarod Wilsona186b4a2007-12-03 13:43:12 -05002236 dma_addr_t d_bus, page_bus;
2237 u32 z, header_z, rest;
David Moorebcee8932007-12-19 15:26:38 -05002238 int i, j, length;
2239 int page, offset, packet_count, header_size, payload_per_buffer;
Jarod Wilsona186b4a2007-12-03 13:43:12 -05002240
2241 /*
David Moore1aa292b2008-07-22 23:23:40 -07002242 * The OHCI controller puts the isochronous header and trailer in the
2243 * buffer, so we need at least 8 bytes.
Jarod Wilsona186b4a2007-12-03 13:43:12 -05002244 */
2245 packet_count = p->header_length / ctx->base.header_size;
David Moore1aa292b2008-07-22 23:23:40 -07002246 header_size = max(ctx->base.header_size, (size_t)8);
Jarod Wilsona186b4a2007-12-03 13:43:12 -05002247
2248 /* Get header size in number of descriptors. */
2249 header_z = DIV_ROUND_UP(header_size, sizeof(*d));
2250 page = payload >> PAGE_SHIFT;
2251 offset = payload & ~PAGE_MASK;
David Moorebcee8932007-12-19 15:26:38 -05002252 payload_per_buffer = p->payload_length / packet_count;
Jarod Wilsona186b4a2007-12-03 13:43:12 -05002253
2254 for (i = 0; i < packet_count; i++) {
2255 /* d points to the header descriptor */
David Moorebcee8932007-12-19 15:26:38 -05002256 z = DIV_ROUND_UP(payload_per_buffer + offset, PAGE_SIZE) + 1;
Jarod Wilsona186b4a2007-12-03 13:43:12 -05002257 d = context_get_descriptors(&ctx->context,
David Moorebcee8932007-12-19 15:26:38 -05002258 z + header_z, &d_bus);
Jarod Wilsona186b4a2007-12-03 13:43:12 -05002259 if (d == NULL)
2260 return -ENOMEM;
2261
David Moorebcee8932007-12-19 15:26:38 -05002262 d->control = cpu_to_le16(DESCRIPTOR_STATUS |
2263 DESCRIPTOR_INPUT_MORE);
2264 if (p->skip && i == 0)
2265 d->control |= cpu_to_le16(DESCRIPTOR_WAIT);
Jarod Wilsona186b4a2007-12-03 13:43:12 -05002266 d->req_count = cpu_to_le16(header_size);
2267 d->res_count = d->req_count;
David Moorebcee8932007-12-19 15:26:38 -05002268 d->transfer_status = 0;
Jarod Wilsona186b4a2007-12-03 13:43:12 -05002269 d->data_address = cpu_to_le32(d_bus + (z * sizeof(*d)));
2270
David Moorebcee8932007-12-19 15:26:38 -05002271 rest = payload_per_buffer;
2272 for (j = 1; j < z; j++) {
2273 pd = d + j;
2274 pd->control = cpu_to_le16(DESCRIPTOR_STATUS |
2275 DESCRIPTOR_INPUT_MORE);
2276
2277 if (offset + rest < PAGE_SIZE)
2278 length = rest;
2279 else
2280 length = PAGE_SIZE - offset;
2281 pd->req_count = cpu_to_le16(length);
2282 pd->res_count = pd->req_count;
2283 pd->transfer_status = 0;
2284
2285 page_bus = page_private(buffer->pages[page]);
2286 pd->data_address = cpu_to_le32(page_bus + offset);
2287
2288 offset = (offset + length) & ~PAGE_MASK;
2289 rest -= length;
2290 if (offset == 0)
2291 page++;
2292 }
Jarod Wilsona186b4a2007-12-03 13:43:12 -05002293 pd->control = cpu_to_le16(DESCRIPTOR_STATUS |
2294 DESCRIPTOR_INPUT_LAST |
2295 DESCRIPTOR_BRANCH_ALWAYS);
David Moorebcee8932007-12-19 15:26:38 -05002296 if (p->interrupt && i == packet_count - 1)
Jarod Wilsona186b4a2007-12-03 13:43:12 -05002297 pd->control |= cpu_to_le16(DESCRIPTOR_IRQ_ALWAYS);
2298
Jarod Wilsona186b4a2007-12-03 13:43:12 -05002299 context_append(&ctx->context, d, z, header_z);
2300 }
2301
2302 return 0;
2303}
2304
Stefan Richter53dca512008-12-14 21:47:04 +01002305static int ohci_queue_iso(struct fw_iso_context *base,
2306 struct fw_iso_packet *packet,
2307 struct fw_iso_buffer *buffer,
2308 unsigned long payload)
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -05002309{
Kristian Høgsberge364cf42007-02-16 17:34:49 -05002310 struct iso_context *ctx = container_of(base, struct iso_context, base);
David Moorefe5ca632008-01-06 17:21:41 -05002311 unsigned long flags;
Stefan Richter2dbd7d72008-12-14 21:45:45 +01002312 int ret;
Kristian Høgsberge364cf42007-02-16 17:34:49 -05002313
David Moorefe5ca632008-01-06 17:21:41 -05002314 spin_lock_irqsave(&ctx->context.ohci->lock, flags);
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -05002315 if (base->type == FW_ISO_CONTEXT_TRANSMIT)
Stefan Richter2dbd7d72008-12-14 21:45:45 +01002316 ret = ohci_queue_iso_transmit(base, packet, buffer, payload);
Stefan Richter95984f62008-07-22 18:41:10 +02002317 else if (ctx->context.ohci->use_dualbuffer)
Stefan Richter2dbd7d72008-12-14 21:45:45 +01002318 ret = ohci_queue_iso_receive_dualbuffer(base, packet,
2319 buffer, payload);
Kristian Høgsberge364cf42007-02-16 17:34:49 -05002320 else
Stefan Richter2dbd7d72008-12-14 21:45:45 +01002321 ret = ohci_queue_iso_receive_packet_per_buffer(base, packet,
2322 buffer, payload);
David Moorefe5ca632008-01-06 17:21:41 -05002323 spin_unlock_irqrestore(&ctx->context.ohci->lock, flags);
2324
Stefan Richter2dbd7d72008-12-14 21:45:45 +01002325 return ret;
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -05002326}
2327
Stefan Richter21ebcd12007-01-14 15:29:07 +01002328static const struct fw_card_driver ohci_driver = {
Kristian Høgsberged568912006-12-19 19:58:35 -05002329 .enable = ohci_enable,
2330 .update_phy_reg = ohci_update_phy_reg,
2331 .set_config_rom = ohci_set_config_rom,
2332 .send_request = ohci_send_request,
2333 .send_response = ohci_send_response,
Kristian Høgsberg730c32f2007-02-06 14:49:32 -05002334 .cancel_packet = ohci_cancel_packet,
Kristian Høgsberged568912006-12-19 19:58:35 -05002335 .enable_phys_dma = ohci_enable_phys_dma,
Kristian Høgsbergd60d7f12007-03-07 12:12:56 -05002336 .get_bus_time = ohci_get_bus_time,
Kristian Høgsberged568912006-12-19 19:58:35 -05002337
2338 .allocate_iso_context = ohci_allocate_iso_context,
2339 .free_iso_context = ohci_free_iso_context,
2340 .queue_iso = ohci_queue_iso,
Kristian Høgsberg69cdb722007-02-16 17:34:41 -05002341 .start_iso = ohci_start_iso,
Kristian Høgsbergb8295662007-02-16 17:34:42 -05002342 .stop_iso = ohci_stop_iso,
Kristian Høgsberged568912006-12-19 19:58:35 -05002343};
2344
Stefan Richter2ed0f182008-03-01 12:35:29 +01002345#ifdef CONFIG_PPC_PMAC
2346static void ohci_pmac_on(struct pci_dev *dev)
2347{
2348 if (machine_is(powermac)) {
2349 struct device_node *ofn = pci_device_to_OF_node(dev);
2350
2351 if (ofn) {
2352 pmac_call_feature(PMAC_FTR_1394_CABLE_POWER, ofn, 0, 1);
2353 pmac_call_feature(PMAC_FTR_1394_ENABLE, ofn, 0, 1);
2354 }
2355 }
2356}
2357
2358static void ohci_pmac_off(struct pci_dev *dev)
2359{
2360 if (machine_is(powermac)) {
2361 struct device_node *ofn = pci_device_to_OF_node(dev);
2362
2363 if (ofn) {
2364 pmac_call_feature(PMAC_FTR_1394_ENABLE, ofn, 0, 0);
2365 pmac_call_feature(PMAC_FTR_1394_CABLE_POWER, ofn, 0, 0);
2366 }
2367 }
2368}
2369#else
2370#define ohci_pmac_on(dev)
2371#define ohci_pmac_off(dev)
2372#endif /* CONFIG_PPC_PMAC */
2373
Stefan Richter53dca512008-12-14 21:47:04 +01002374static int __devinit pci_probe(struct pci_dev *dev,
2375 const struct pci_device_id *ent)
Kristian Høgsberged568912006-12-19 19:58:35 -05002376{
2377 struct fw_ohci *ohci;
Stefan Richter95984f62008-07-22 18:41:10 +02002378 u32 bus_options, max_receive, link_speed, version;
Kristian Høgsberged568912006-12-19 19:58:35 -05002379 u64 guid;
Kristian Høgsbergd79406d2007-05-09 19:23:15 -04002380 int err;
Kristian Høgsberged568912006-12-19 19:58:35 -05002381 size_t size;
2382
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -04002383 ohci = kzalloc(sizeof(*ohci), GFP_KERNEL);
Kristian Høgsberged568912006-12-19 19:58:35 -05002384 if (ohci == NULL) {
Stefan Richter7007a072008-10-26 09:50:31 +01002385 err = -ENOMEM;
2386 goto fail;
Kristian Høgsberged568912006-12-19 19:58:35 -05002387 }
2388
2389 fw_card_initialize(&ohci->card, &ohci_driver, &dev->dev);
2390
Stefan Richter130d5492008-03-24 20:55:28 +01002391 ohci_pmac_on(dev);
2392
Kristian Høgsbergd79406d2007-05-09 19:23:15 -04002393 err = pci_enable_device(dev);
2394 if (err) {
Stefan Richter7007a072008-10-26 09:50:31 +01002395 fw_error("Failed to enable OHCI hardware\n");
Stefan Richterbd7dee62008-02-24 18:59:55 +01002396 goto fail_free;
Kristian Høgsberged568912006-12-19 19:58:35 -05002397 }
2398
2399 pci_set_master(dev);
2400 pci_write_config_dword(dev, OHCI1394_PCI_HCI_Control, 0);
2401 pci_set_drvdata(dev, ohci);
2402
2403 spin_lock_init(&ohci->lock);
2404
2405 tasklet_init(&ohci->bus_reset_tasklet,
2406 bus_reset_tasklet, (unsigned long)ohci);
2407
Kristian Høgsbergd79406d2007-05-09 19:23:15 -04002408 err = pci_request_region(dev, 0, ohci_driver_name);
2409 if (err) {
Kristian Høgsberged568912006-12-19 19:58:35 -05002410 fw_error("MMIO resource unavailable\n");
Kristian Høgsbergd79406d2007-05-09 19:23:15 -04002411 goto fail_disable;
Kristian Høgsberged568912006-12-19 19:58:35 -05002412 }
2413
2414 ohci->registers = pci_iomap(dev, 0, OHCI1394_REGISTER_SIZE);
2415 if (ohci->registers == NULL) {
2416 fw_error("Failed to remap registers\n");
Kristian Høgsbergd79406d2007-05-09 19:23:15 -04002417 err = -ENXIO;
2418 goto fail_iomem;
Kristian Høgsberged568912006-12-19 19:58:35 -05002419 }
2420
Stefan Richter95984f62008-07-22 18:41:10 +02002421 version = reg_read(ohci, OHCI1394_Version) & 0x00ff00ff;
2422 ohci->use_dualbuffer = version >= OHCI_VERSION_1_1;
2423
2424/* x86-32 currently doesn't use highmem for dma_alloc_coherent */
2425#if !defined(CONFIG_X86_32)
2426 /* dual-buffer mode is broken with descriptor addresses above 2G */
2427 if (dev->vendor == PCI_VENDOR_ID_TI &&
2428 dev->device == PCI_DEVICE_ID_TI_TSB43AB22)
2429 ohci->use_dualbuffer = false;
2430#endif
2431
2432#if defined(CONFIG_PPC_PMAC) && defined(CONFIG_PPC32)
2433 ohci->old_uninorth = dev->vendor == PCI_VENDOR_ID_APPLE &&
2434 dev->device == PCI_DEVICE_ID_APPLE_UNI_N_FW;
2435#endif
2436 ohci->bus_reset_packet_quirk = dev->vendor == PCI_VENDOR_ID_TI;
2437
Kristian Høgsberged568912006-12-19 19:58:35 -05002438 ar_context_init(&ohci->ar_request_ctx, ohci,
2439 OHCI1394_AsReqRcvContextControlSet);
2440
2441 ar_context_init(&ohci->ar_response_ctx, ohci,
2442 OHCI1394_AsRspRcvContextControlSet);
2443
David Moorefe5ca632008-01-06 17:21:41 -05002444 context_init(&ohci->at_request_ctx, ohci,
Kristian Høgsbergf319b6a2007-03-07 12:12:49 -05002445 OHCI1394_AsReqTrContextControlSet, handle_at_packet);
Kristian Høgsberged568912006-12-19 19:58:35 -05002446
David Moorefe5ca632008-01-06 17:21:41 -05002447 context_init(&ohci->at_response_ctx, ohci,
Kristian Høgsbergf319b6a2007-03-07 12:12:49 -05002448 OHCI1394_AsRspTrContextControlSet, handle_at_packet);
Kristian Høgsberged568912006-12-19 19:58:35 -05002449
Kristian Høgsberged568912006-12-19 19:58:35 -05002450 reg_write(ohci, OHCI1394_IsoRecvIntMaskSet, ~0);
2451 ohci->it_context_mask = reg_read(ohci, OHCI1394_IsoRecvIntMaskSet);
2452 reg_write(ohci, OHCI1394_IsoRecvIntMaskClear, ~0);
2453 size = sizeof(struct iso_context) * hweight32(ohci->it_context_mask);
2454 ohci->it_context_list = kzalloc(size, GFP_KERNEL);
2455
2456 reg_write(ohci, OHCI1394_IsoXmitIntMaskSet, ~0);
Stefan Richter4817ed22008-12-21 16:39:46 +01002457 ohci->ir_context_channels = ~0ULL;
Kristian Høgsberged568912006-12-19 19:58:35 -05002458 ohci->ir_context_mask = reg_read(ohci, OHCI1394_IsoXmitIntMaskSet);
2459 reg_write(ohci, OHCI1394_IsoXmitIntMaskClear, ~0);
2460 size = sizeof(struct iso_context) * hweight32(ohci->ir_context_mask);
2461 ohci->ir_context_list = kzalloc(size, GFP_KERNEL);
2462
2463 if (ohci->it_context_list == NULL || ohci->ir_context_list == NULL) {
Kristian Høgsbergd79406d2007-05-09 19:23:15 -04002464 err = -ENOMEM;
Stefan Richter7007a072008-10-26 09:50:31 +01002465 goto fail_contexts;
Kristian Høgsberged568912006-12-19 19:58:35 -05002466 }
2467
2468 /* self-id dma buffer allocation */
2469 ohci->self_id_cpu = dma_alloc_coherent(ohci->card.device,
2470 SELF_ID_BUF_SIZE,
2471 &ohci->self_id_bus,
2472 GFP_KERNEL);
2473 if (ohci->self_id_cpu == NULL) {
Kristian Høgsbergd79406d2007-05-09 19:23:15 -04002474 err = -ENOMEM;
Stefan Richter7007a072008-10-26 09:50:31 +01002475 goto fail_contexts;
Kristian Høgsberged568912006-12-19 19:58:35 -05002476 }
2477
Kristian Høgsberged568912006-12-19 19:58:35 -05002478 bus_options = reg_read(ohci, OHCI1394_BusOptions);
2479 max_receive = (bus_options >> 12) & 0xf;
2480 link_speed = bus_options & 0x7;
2481 guid = ((u64) reg_read(ohci, OHCI1394_GUIDHi) << 32) |
2482 reg_read(ohci, OHCI1394_GUIDLo);
2483
Kristian Høgsbergd79406d2007-05-09 19:23:15 -04002484 err = fw_card_add(&ohci->card, max_receive, link_speed, guid);
Stefan Richtere1eff7a2009-02-03 17:55:19 +01002485 if (err)
Kristian Høgsbergd79406d2007-05-09 19:23:15 -04002486 goto fail_self_id;
Kristian Høgsberged568912006-12-19 19:58:35 -05002487
Kristian Høgsberg500be722007-02-16 17:34:43 -05002488 fw_notify("Added fw-ohci device %s, OHCI version %x.%x\n",
Kay Sieversa1f64812008-10-30 01:41:56 +01002489 dev_name(&dev->dev), version >> 16, version & 0xff);
Stefan Richtere1eff7a2009-02-03 17:55:19 +01002490
Kristian Høgsberged568912006-12-19 19:58:35 -05002491 return 0;
Kristian Høgsbergd79406d2007-05-09 19:23:15 -04002492
2493 fail_self_id:
2494 dma_free_coherent(ohci->card.device, SELF_ID_BUF_SIZE,
2495 ohci->self_id_cpu, ohci->self_id_bus);
Stefan Richter7007a072008-10-26 09:50:31 +01002496 fail_contexts:
Kristian Høgsbergd79406d2007-05-09 19:23:15 -04002497 kfree(ohci->ir_context_list);
Stefan Richter7007a072008-10-26 09:50:31 +01002498 kfree(ohci->it_context_list);
2499 context_release(&ohci->at_response_ctx);
2500 context_release(&ohci->at_request_ctx);
2501 ar_context_release(&ohci->ar_response_ctx);
2502 ar_context_release(&ohci->ar_request_ctx);
Kristian Høgsbergd79406d2007-05-09 19:23:15 -04002503 pci_iounmap(dev, ohci->registers);
2504 fail_iomem:
2505 pci_release_region(dev, 0);
2506 fail_disable:
2507 pci_disable_device(dev);
Stefan Richterbd7dee62008-02-24 18:59:55 +01002508 fail_free:
2509 kfree(&ohci->card);
Stefan Richter130d5492008-03-24 20:55:28 +01002510 ohci_pmac_off(dev);
Stefan Richter7007a072008-10-26 09:50:31 +01002511 fail:
2512 if (err == -ENOMEM)
2513 fw_error("Out of memory\n");
Kristian Høgsbergd79406d2007-05-09 19:23:15 -04002514
2515 return err;
Kristian Høgsberged568912006-12-19 19:58:35 -05002516}
2517
2518static void pci_remove(struct pci_dev *dev)
2519{
2520 struct fw_ohci *ohci;
2521
2522 ohci = pci_get_drvdata(dev);
Kristian Høgsberge254a4b2007-03-07 12:12:38 -05002523 reg_write(ohci, OHCI1394_IntMaskClear, ~0);
2524 flush_writes(ohci);
Kristian Høgsberged568912006-12-19 19:58:35 -05002525 fw_core_remove_card(&ohci->card);
2526
Kristian Høgsbergc781c062007-05-07 20:33:32 -04002527 /*
2528 * FIXME: Fail all pending packets here, now that the upper
2529 * layers can't queue any more.
2530 */
Kristian Høgsberged568912006-12-19 19:58:35 -05002531
2532 software_reset(ohci);
2533 free_irq(dev->irq, ohci);
Jay Fenlasona55709b2008-10-22 15:59:42 -04002534
2535 if (ohci->next_config_rom && ohci->next_config_rom != ohci->config_rom)
2536 dma_free_coherent(ohci->card.device, CONFIG_ROM_SIZE,
2537 ohci->next_config_rom, ohci->next_config_rom_bus);
2538 if (ohci->config_rom)
2539 dma_free_coherent(ohci->card.device, CONFIG_ROM_SIZE,
2540 ohci->config_rom, ohci->config_rom_bus);
Kristian Høgsbergd79406d2007-05-09 19:23:15 -04002541 dma_free_coherent(ohci->card.device, SELF_ID_BUF_SIZE,
2542 ohci->self_id_cpu, ohci->self_id_bus);
Jay Fenlasona55709b2008-10-22 15:59:42 -04002543 ar_context_release(&ohci->ar_request_ctx);
2544 ar_context_release(&ohci->ar_response_ctx);
2545 context_release(&ohci->at_request_ctx);
2546 context_release(&ohci->at_response_ctx);
Kristian Høgsbergd79406d2007-05-09 19:23:15 -04002547 kfree(ohci->it_context_list);
2548 kfree(ohci->ir_context_list);
2549 pci_iounmap(dev, ohci->registers);
2550 pci_release_region(dev, 0);
2551 pci_disable_device(dev);
Stefan Richterbd7dee62008-02-24 18:59:55 +01002552 kfree(&ohci->card);
Stefan Richter2ed0f182008-03-01 12:35:29 +01002553 ohci_pmac_off(dev);
Stefan Richterea8d0062008-03-01 02:42:56 +01002554
Kristian Høgsberged568912006-12-19 19:58:35 -05002555 fw_notify("Removed fw-ohci device.\n");
2556}
2557
Kristian Høgsberg2aef4692007-05-30 19:06:35 -04002558#ifdef CONFIG_PM
Stefan Richter2ed0f182008-03-01 12:35:29 +01002559static int pci_suspend(struct pci_dev *dev, pm_message_t state)
Kristian Høgsberg2aef4692007-05-30 19:06:35 -04002560{
Stefan Richter2ed0f182008-03-01 12:35:29 +01002561 struct fw_ohci *ohci = pci_get_drvdata(dev);
Kristian Høgsberg2aef4692007-05-30 19:06:35 -04002562 int err;
2563
2564 software_reset(ohci);
Stefan Richter2ed0f182008-03-01 12:35:29 +01002565 free_irq(dev->irq, ohci);
2566 err = pci_save_state(dev);
Kristian Høgsberg2aef4692007-05-30 19:06:35 -04002567 if (err) {
Stefan Richter8a8cea22007-06-09 19:26:22 +02002568 fw_error("pci_save_state failed\n");
Kristian Høgsberg2aef4692007-05-30 19:06:35 -04002569 return err;
2570 }
Stefan Richter2ed0f182008-03-01 12:35:29 +01002571 err = pci_set_power_state(dev, pci_choose_state(dev, state));
Stefan Richter55111422007-09-06 09:50:30 +02002572 if (err)
2573 fw_error("pci_set_power_state failed with %d\n", err);
Stefan Richter2ed0f182008-03-01 12:35:29 +01002574 ohci_pmac_off(dev);
Stefan Richterea8d0062008-03-01 02:42:56 +01002575
Kristian Høgsberg2aef4692007-05-30 19:06:35 -04002576 return 0;
2577}
2578
Stefan Richter2ed0f182008-03-01 12:35:29 +01002579static int pci_resume(struct pci_dev *dev)
Kristian Høgsberg2aef4692007-05-30 19:06:35 -04002580{
Stefan Richter2ed0f182008-03-01 12:35:29 +01002581 struct fw_ohci *ohci = pci_get_drvdata(dev);
Kristian Høgsberg2aef4692007-05-30 19:06:35 -04002582 int err;
2583
Stefan Richter2ed0f182008-03-01 12:35:29 +01002584 ohci_pmac_on(dev);
2585 pci_set_power_state(dev, PCI_D0);
2586 pci_restore_state(dev);
2587 err = pci_enable_device(dev);
Kristian Høgsberg2aef4692007-05-30 19:06:35 -04002588 if (err) {
Stefan Richter8a8cea22007-06-09 19:26:22 +02002589 fw_error("pci_enable_device failed\n");
Kristian Høgsberg2aef4692007-05-30 19:06:35 -04002590 return err;
2591 }
2592
Kristian Høgsberg0bd243c2007-06-05 19:27:05 -04002593 return ohci_enable(&ohci->card, NULL, 0);
Kristian Høgsberg2aef4692007-05-30 19:06:35 -04002594}
2595#endif
2596
Kristian Høgsberged568912006-12-19 19:58:35 -05002597static struct pci_device_id pci_table[] = {
2598 { PCI_DEVICE_CLASS(PCI_CLASS_SERIAL_FIREWIRE_OHCI, ~0) },
2599 { }
2600};
2601
2602MODULE_DEVICE_TABLE(pci, pci_table);
2603
2604static struct pci_driver fw_ohci_pci_driver = {
2605 .name = ohci_driver_name,
2606 .id_table = pci_table,
2607 .probe = pci_probe,
2608 .remove = pci_remove,
Kristian Høgsberg2aef4692007-05-30 19:06:35 -04002609#ifdef CONFIG_PM
2610 .resume = pci_resume,
2611 .suspend = pci_suspend,
2612#endif
Kristian Høgsberged568912006-12-19 19:58:35 -05002613};
2614
2615MODULE_AUTHOR("Kristian Hoegsberg <krh@bitplanet.net>");
2616MODULE_DESCRIPTION("Driver for PCI OHCI IEEE1394 controllers");
2617MODULE_LICENSE("GPL");
2618
Olaf Hering1e4c7b02007-05-05 23:17:13 +02002619/* Provide a module alias so root-on-sbp2 initrds don't break. */
2620#ifndef CONFIG_IEEE1394_OHCI1394_MODULE
2621MODULE_ALIAS("ohci1394");
2622#endif
2623
Kristian Høgsberged568912006-12-19 19:58:35 -05002624static int __init fw_ohci_init(void)
2625{
2626 return pci_register_driver(&fw_ohci_pci_driver);
2627}
2628
2629static void __exit fw_ohci_cleanup(void)
2630{
2631 pci_unregister_driver(&fw_ohci_pci_driver);
2632}
2633
2634module_init(fw_ohci_init);
2635module_exit(fw_ohci_cleanup);