fw-ohci.c 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431
  1. /* -*- c-basic-offset: 8 -*-
  2. *
  3. * fw-ohci.c - Driver for OHCI 1394 boards
  4. * Copyright (C) 2003-2006 Kristian Hoegsberg <krh@bitplanet.net>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  19. */
  20. #include <linux/kernel.h>
  21. #include <linux/module.h>
  22. #include <linux/init.h>
  23. #include <linux/interrupt.h>
  24. #include <linux/pci.h>
  25. #include <linux/delay.h>
  26. #include <linux/poll.h>
  27. #include <linux/dma-mapping.h>
  28. #include <asm/uaccess.h>
  29. #include <asm/semaphore.h>
  30. #include "fw-transaction.h"
  31. #include "fw-ohci.h"
  32. #define descriptor_output_more 0
  33. #define descriptor_output_last (1 << 12)
  34. #define descriptor_input_more (2 << 12)
  35. #define descriptor_input_last (3 << 12)
  36. #define descriptor_status (1 << 11)
  37. #define descriptor_key_immediate (2 << 8)
  38. #define descriptor_ping (1 << 7)
  39. #define descriptor_yy (1 << 6)
  40. #define descriptor_no_irq (0 << 4)
  41. #define descriptor_irq_error (1 << 4)
  42. #define descriptor_irq_always (3 << 4)
  43. #define descriptor_branch_always (3 << 2)
  44. struct descriptor {
  45. __le16 req_count;
  46. __le16 control;
  47. __le32 data_address;
  48. __le32 branch_address;
  49. __le16 res_count;
  50. __le16 transfer_status;
  51. } __attribute__((aligned(16)));
  52. struct ar_context {
  53. struct fw_ohci *ohci;
  54. struct descriptor descriptor;
  55. __le32 buffer[512];
  56. dma_addr_t descriptor_bus;
  57. dma_addr_t buffer_bus;
  58. u32 command_ptr;
  59. u32 control_set;
  60. u32 control_clear;
  61. struct tasklet_struct tasklet;
  62. };
  63. struct at_context {
  64. struct fw_ohci *ohci;
  65. dma_addr_t descriptor_bus;
  66. dma_addr_t buffer_bus;
  67. struct list_head list;
  68. struct {
  69. struct descriptor more;
  70. __le32 header[4];
  71. struct descriptor last;
  72. } d;
  73. u32 command_ptr;
  74. u32 control_set;
  75. u32 control_clear;
  76. struct tasklet_struct tasklet;
  77. };
  78. #define it_header_sy(v) ((v) << 0)
  79. #define it_header_tcode(v) ((v) << 4)
  80. #define it_header_channel(v) ((v) << 8)
  81. #define it_header_tag(v) ((v) << 14)
  82. #define it_header_speed(v) ((v) << 16)
  83. #define it_header_data_length(v) ((v) << 16)
  84. struct iso_context {
  85. struct fw_iso_context base;
  86. struct tasklet_struct tasklet;
  87. u32 control_set;
  88. u32 control_clear;
  89. u32 command_ptr;
  90. u32 context_match;
  91. struct descriptor *buffer;
  92. dma_addr_t buffer_bus;
  93. struct descriptor *head_descriptor;
  94. struct descriptor *tail_descriptor;
  95. struct descriptor *tail_descriptor_last;
  96. struct descriptor *prev_descriptor;
  97. };
  98. #define CONFIG_ROM_SIZE 1024
  99. struct fw_ohci {
  100. struct fw_card card;
  101. __iomem char *registers;
  102. dma_addr_t self_id_bus;
  103. __le32 *self_id_cpu;
  104. struct tasklet_struct bus_reset_tasklet;
  105. int node_id;
  106. int generation;
  107. int request_generation;
  108. /* Spinlock for accessing fw_ohci data. Never call out of
  109. * this driver with this lock held. */
  110. spinlock_t lock;
  111. u32 self_id_buffer[512];
  112. /* Config rom buffers */
  113. __be32 *config_rom;
  114. dma_addr_t config_rom_bus;
  115. __be32 *next_config_rom;
  116. dma_addr_t next_config_rom_bus;
  117. u32 next_header;
  118. struct ar_context ar_request_ctx;
  119. struct ar_context ar_response_ctx;
  120. struct at_context at_request_ctx;
  121. struct at_context at_response_ctx;
  122. u32 it_context_mask;
  123. struct iso_context *it_context_list;
  124. u32 ir_context_mask;
  125. struct iso_context *ir_context_list;
  126. };
  127. static inline struct fw_ohci *fw_ohci(struct fw_card *card)
  128. {
  129. return container_of(card, struct fw_ohci, card);
  130. }
  131. #define CONTEXT_CYCLE_MATCH_ENABLE 0x80000000
  132. #define CONTEXT_RUN 0x8000
  133. #define CONTEXT_WAKE 0x1000
  134. #define CONTEXT_DEAD 0x0800
  135. #define CONTEXT_ACTIVE 0x0400
  136. #define OHCI1394_MAX_AT_REQ_RETRIES 0x2
  137. #define OHCI1394_MAX_AT_RESP_RETRIES 0x2
  138. #define OHCI1394_MAX_PHYS_RESP_RETRIES 0x8
  139. #define FW_OHCI_MAJOR 240
  140. #define OHCI1394_REGISTER_SIZE 0x800
  141. #define OHCI_LOOP_COUNT 500
  142. #define OHCI1394_PCI_HCI_Control 0x40
  143. #define SELF_ID_BUF_SIZE 0x800
  144. static char ohci_driver_name[] = KBUILD_MODNAME;
  145. static inline void reg_write(const struct fw_ohci *ohci, int offset, u32 data)
  146. {
  147. writel(data, ohci->registers + offset);
  148. }
  149. static inline u32 reg_read(const struct fw_ohci *ohci, int offset)
  150. {
  151. return readl(ohci->registers + offset);
  152. }
  153. static inline void flush_writes(const struct fw_ohci *ohci)
  154. {
  155. /* Do a dummy read to flush writes. */
  156. reg_read(ohci, OHCI1394_Version);
  157. }
  158. static int
  159. ohci_update_phy_reg(struct fw_card *card, int addr,
  160. int clear_bits, int set_bits)
  161. {
  162. struct fw_ohci *ohci = fw_ohci(card);
  163. u32 val, old;
  164. reg_write(ohci, OHCI1394_PhyControl, OHCI1394_PhyControl_Read(addr));
  165. msleep(2);
  166. val = reg_read(ohci, OHCI1394_PhyControl);
  167. if ((val & OHCI1394_PhyControl_ReadDone) == 0) {
  168. fw_error("failed to set phy reg bits.\n");
  169. return -EBUSY;
  170. }
  171. old = OHCI1394_PhyControl_ReadData(val);
  172. old = (old & ~clear_bits) | set_bits;
  173. reg_write(ohci, OHCI1394_PhyControl,
  174. OHCI1394_PhyControl_Write(addr, old));
  175. return 0;
  176. }
  177. static void ar_context_run(struct ar_context *ctx)
  178. {
  179. reg_write(ctx->ohci, ctx->command_ptr, ctx->descriptor_bus | 1);
  180. reg_write(ctx->ohci, ctx->control_set, CONTEXT_RUN);
  181. flush_writes(ctx->ohci);
  182. }
  183. static void ar_context_tasklet(unsigned long data)
  184. {
  185. struct ar_context *ctx = (struct ar_context *)data;
  186. struct fw_ohci *ohci = ctx->ohci;
  187. struct fw_packet p;
  188. u32 status, length, tcode;
  189. /* FIXME: What to do about evt_* errors? */
  190. length = le16_to_cpu(ctx->descriptor.req_count) -
  191. le16_to_cpu(ctx->descriptor.res_count) - 4;
  192. status = le32_to_cpu(ctx->buffer[length / 4]);
  193. p.ack = ((status >> 16) & 0x1f) - 16;
  194. p.speed = (status >> 21) & 0x7;
  195. p.timestamp = status & 0xffff;
  196. p.generation = ohci->request_generation;
  197. p.header[0] = le32_to_cpu(ctx->buffer[0]);
  198. p.header[1] = le32_to_cpu(ctx->buffer[1]);
  199. p.header[2] = le32_to_cpu(ctx->buffer[2]);
  200. tcode = (p.header[0] >> 4) & 0x0f;
  201. switch (tcode) {
  202. case TCODE_WRITE_QUADLET_REQUEST:
  203. case TCODE_READ_QUADLET_RESPONSE:
  204. p.header[3] = ctx->buffer[3];
  205. p.header_length = 16;
  206. break;
  207. case TCODE_WRITE_BLOCK_REQUEST:
  208. case TCODE_READ_BLOCK_REQUEST :
  209. case TCODE_READ_BLOCK_RESPONSE:
  210. case TCODE_LOCK_REQUEST:
  211. case TCODE_LOCK_RESPONSE:
  212. p.header[3] = le32_to_cpu(ctx->buffer[3]);
  213. p.header_length = 16;
  214. break;
  215. case TCODE_WRITE_RESPONSE:
  216. case TCODE_READ_QUADLET_REQUEST:
  217. p.header_length = 12;
  218. break;
  219. }
  220. p.payload = (void *) ctx->buffer + p.header_length;
  221. p.payload_length = length - p.header_length;
  222. /* The OHCI bus reset handler synthesizes a phy packet with
  223. * the new generation number when a bus reset happens (see
  224. * section 8.4.2.3). This helps us determine when a request
  225. * was received and make sure we send the response in the same
  226. * generation. We only need this for requests; for responses
  227. * we use the unique tlabel for finding the matching
  228. * request. */
  229. if (p.ack + 16 == 0x09)
  230. ohci->request_generation = (ctx->buffer[2] >> 16) & 0xff;
  231. else if (ctx == &ohci->ar_request_ctx)
  232. fw_core_handle_request(&ohci->card, &p);
  233. else
  234. fw_core_handle_response(&ohci->card, &p);
  235. ctx->descriptor.data_address = cpu_to_le32(ctx->buffer_bus);
  236. ctx->descriptor.req_count = cpu_to_le16(sizeof ctx->buffer);
  237. ctx->descriptor.res_count = cpu_to_le16(sizeof ctx->buffer);
  238. dma_sync_single_for_device(ohci->card.device, ctx->descriptor_bus,
  239. sizeof ctx->descriptor_bus, DMA_TO_DEVICE);
  240. /* FIXME: We stop and restart the ar context here, what if we
  241. * stop while a receive is in progress? Maybe we could just
  242. * loop the context back to itself and use it in buffer fill
  243. * mode as intended... */
  244. reg_write(ctx->ohci, ctx->control_clear, CONTEXT_RUN);
  245. ar_context_run(ctx);
  246. }
  247. static int
  248. ar_context_init(struct ar_context *ctx, struct fw_ohci *ohci, u32 control_set)
  249. {
  250. ctx->descriptor_bus =
  251. dma_map_single(ohci->card.device, &ctx->descriptor,
  252. sizeof ctx->descriptor, DMA_TO_DEVICE);
  253. if (ctx->descriptor_bus == 0)
  254. return -ENOMEM;
  255. if (ctx->descriptor_bus & 0xf)
  256. fw_notify("descriptor not 16-byte aligned: 0x%08lx\n",
  257. (unsigned long)ctx->descriptor_bus);
  258. ctx->buffer_bus =
  259. dma_map_single(ohci->card.device, ctx->buffer,
  260. sizeof ctx->buffer, DMA_FROM_DEVICE);
  261. if (ctx->buffer_bus == 0) {
  262. dma_unmap_single(ohci->card.device, ctx->descriptor_bus,
  263. sizeof ctx->descriptor, DMA_TO_DEVICE);
  264. return -ENOMEM;
  265. }
  266. memset(&ctx->descriptor, 0, sizeof ctx->descriptor);
  267. ctx->descriptor.control = cpu_to_le16(descriptor_input_more |
  268. descriptor_status |
  269. descriptor_branch_always);
  270. ctx->descriptor.req_count = cpu_to_le16(sizeof ctx->buffer);
  271. ctx->descriptor.data_address = cpu_to_le32(ctx->buffer_bus);
  272. ctx->descriptor.res_count = cpu_to_le16(sizeof ctx->buffer);
  273. ctx->control_set = control_set;
  274. ctx->control_clear = control_set + 4;
  275. ctx->command_ptr = control_set + 12;
  276. ctx->ohci = ohci;
  277. tasklet_init(&ctx->tasklet, ar_context_tasklet, (unsigned long)ctx);
  278. ar_context_run(ctx);
  279. return 0;
  280. }
  281. static void
  282. do_packet_callbacks(struct fw_ohci *ohci, struct list_head *list)
  283. {
  284. struct fw_packet *p, *next;
  285. list_for_each_entry_safe(p, next, list, link)
  286. p->callback(p, &ohci->card, p->ack);
  287. }
  288. static void
  289. complete_transmission(struct fw_packet *packet,
  290. int ack, struct list_head *list)
  291. {
  292. list_move_tail(&packet->link, list);
  293. packet->ack = ack;
  294. }
  295. /* This function prepares the first packet in the context queue for
  296. * transmission. Must always be called with the ochi->lock held to
  297. * ensure proper generation handling and locking around packet queue
  298. * manipulation. */
  299. static void
  300. at_context_setup_packet(struct at_context *ctx, struct list_head *list)
  301. {
  302. struct fw_packet *packet;
  303. struct fw_ohci *ohci = ctx->ohci;
  304. int z, tcode;
  305. packet = fw_packet(ctx->list.next);
  306. memset(&ctx->d, 0, sizeof ctx->d);
  307. if (packet->payload_length > 0) {
  308. packet->payload_bus = dma_map_single(ohci->card.device,
  309. packet->payload,
  310. packet->payload_length,
  311. DMA_TO_DEVICE);
  312. if (packet->payload_bus == 0) {
  313. complete_transmission(packet, -ENOMEM, list);
  314. return;
  315. }
  316. ctx->d.more.control =
  317. cpu_to_le16(descriptor_output_more |
  318. descriptor_key_immediate);
  319. ctx->d.more.req_count = cpu_to_le16(packet->header_length);
  320. ctx->d.more.res_count = cpu_to_le16(packet->timestamp);
  321. ctx->d.last.control =
  322. cpu_to_le16(descriptor_output_last |
  323. descriptor_irq_always |
  324. descriptor_branch_always);
  325. ctx->d.last.req_count = cpu_to_le16(packet->payload_length);
  326. ctx->d.last.data_address = cpu_to_le32(packet->payload_bus);
  327. z = 3;
  328. } else {
  329. ctx->d.more.control =
  330. cpu_to_le16(descriptor_output_last |
  331. descriptor_key_immediate |
  332. descriptor_irq_always |
  333. descriptor_branch_always);
  334. ctx->d.more.req_count = cpu_to_le16(packet->header_length);
  335. ctx->d.more.res_count = cpu_to_le16(packet->timestamp);
  336. z = 2;
  337. }
  338. /* The DMA format for asyncronous link packets is different
  339. * from the IEEE1394 layout, so shift the fields around
  340. * accordingly. If header_length is 8, it's a PHY packet, to
  341. * which we need to prepend an extra quadlet. */
  342. if (packet->header_length > 8) {
  343. ctx->d.header[0] = cpu_to_le32((packet->header[0] & 0xffff) |
  344. (packet->speed << 16));
  345. ctx->d.header[1] = cpu_to_le32((packet->header[1] & 0xffff) |
  346. (packet->header[0] & 0xffff0000));
  347. ctx->d.header[2] = cpu_to_le32(packet->header[2]);
  348. tcode = (packet->header[0] >> 4) & 0x0f;
  349. if (TCODE_IS_BLOCK_PACKET(tcode))
  350. ctx->d.header[3] = cpu_to_le32(packet->header[3]);
  351. else
  352. ctx->d.header[3] = packet->header[3];
  353. } else {
  354. ctx->d.header[0] =
  355. cpu_to_le32((OHCI1394_phy_tcode << 4) |
  356. (packet->speed << 16));
  357. ctx->d.header[1] = cpu_to_le32(packet->header[0]);
  358. ctx->d.header[2] = cpu_to_le32(packet->header[1]);
  359. ctx->d.more.req_count = cpu_to_le16(12);
  360. }
  361. /* FIXME: Document how the locking works. */
  362. if (ohci->generation == packet->generation) {
  363. reg_write(ctx->ohci, ctx->command_ptr,
  364. ctx->descriptor_bus | z);
  365. reg_write(ctx->ohci, ctx->control_set,
  366. CONTEXT_RUN | CONTEXT_WAKE);
  367. } else {
  368. /* We dont return error codes from this function; all
  369. * transmission errors are reported through the
  370. * callback. */
  371. complete_transmission(packet, -ESTALE, list);
  372. }
  373. }
  374. static void at_context_stop(struct at_context *ctx)
  375. {
  376. u32 reg;
  377. reg_write(ctx->ohci, ctx->control_clear, CONTEXT_RUN);
  378. reg = reg_read(ctx->ohci, ctx->control_set);
  379. if (reg & CONTEXT_ACTIVE)
  380. fw_notify("Tried to stop context, but it is still active "
  381. "(0x%08x).\n", reg);
  382. }
  383. static void at_context_tasklet(unsigned long data)
  384. {
  385. struct at_context *ctx = (struct at_context *)data;
  386. struct fw_ohci *ohci = ctx->ohci;
  387. struct fw_packet *packet;
  388. LIST_HEAD(list);
  389. unsigned long flags;
  390. int evt;
  391. spin_lock_irqsave(&ohci->lock, flags);
  392. packet = fw_packet(ctx->list.next);
  393. at_context_stop(ctx);
  394. if (packet->payload_length > 0) {
  395. dma_unmap_single(ohci->card.device, packet->payload_bus,
  396. packet->payload_length, DMA_TO_DEVICE);
  397. evt = le16_to_cpu(ctx->d.last.transfer_status) & 0x1f;
  398. packet->timestamp = le16_to_cpu(ctx->d.last.res_count);
  399. }
  400. else {
  401. evt = le16_to_cpu(ctx->d.more.transfer_status) & 0x1f;
  402. packet->timestamp = le16_to_cpu(ctx->d.more.res_count);
  403. }
  404. if (evt < 16) {
  405. switch (evt) {
  406. case OHCI1394_evt_timeout:
  407. /* Async response transmit timed out. */
  408. complete_transmission(packet, -ETIMEDOUT, &list);
  409. break;
  410. case OHCI1394_evt_flushed:
  411. /* The packet was flushed should give same
  412. * error as when we try to use a stale
  413. * generation count. */
  414. complete_transmission(packet, -ESTALE, &list);
  415. break;
  416. case OHCI1394_evt_missing_ack:
  417. /* This would be a higher level software
  418. * error, it is using a valid (current)
  419. * generation count, but the node is not on
  420. * the bus. */
  421. complete_transmission(packet, -ENODEV, &list);
  422. break;
  423. default:
  424. complete_transmission(packet, -EIO, &list);
  425. break;
  426. }
  427. } else
  428. complete_transmission(packet, evt - 16, &list);
  429. /* If more packets are queued, set up the next one. */
  430. if (!list_empty(&ctx->list))
  431. at_context_setup_packet(ctx, &list);
  432. spin_unlock_irqrestore(&ohci->lock, flags);
  433. do_packet_callbacks(ohci, &list);
  434. }
  435. static int
  436. at_context_init(struct at_context *ctx, struct fw_ohci *ohci, u32 control_set)
  437. {
  438. INIT_LIST_HEAD(&ctx->list);
  439. ctx->descriptor_bus =
  440. dma_map_single(ohci->card.device, &ctx->d,
  441. sizeof ctx->d, DMA_TO_DEVICE);
  442. if (ctx->descriptor_bus == 0)
  443. return -ENOMEM;
  444. ctx->control_set = control_set;
  445. ctx->control_clear = control_set + 4;
  446. ctx->command_ptr = control_set + 12;
  447. ctx->ohci = ohci;
  448. tasklet_init(&ctx->tasklet, at_context_tasklet, (unsigned long)ctx);
  449. return 0;
  450. }
  451. #define header_get_destination(q) (((q) >> 16) & 0xffff)
  452. static void
  453. at_context_transmit(struct at_context *ctx, struct fw_packet *packet)
  454. {
  455. LIST_HEAD(list);
  456. unsigned long flags;
  457. int local;
  458. spin_lock_irqsave(&ctx->ohci->lock, flags);
  459. if (header_get_destination(packet->header[0]) == ctx->ohci->node_id &&
  460. ctx->ohci->generation == packet->generation) {
  461. local = 1;
  462. } else {
  463. list_add_tail(&packet->link, &ctx->list);
  464. if (ctx->list.next == &packet->link)
  465. at_context_setup_packet(ctx, &list);
  466. local = 0;
  467. }
  468. spin_unlock_irqrestore(&ctx->ohci->lock, flags);
  469. do_packet_callbacks(ctx->ohci, &list);
  470. if (local) {
  471. packet->ack = ACK_PENDING;
  472. packet->callback(packet, &ctx->ohci->card, packet->ack);
  473. if (ctx == &ctx->ohci->at_request_ctx)
  474. fw_core_handle_request(&ctx->ohci->card, packet);
  475. else
  476. fw_core_handle_response(&ctx->ohci->card, packet);
  477. }
  478. }
  479. static void bus_reset_tasklet(unsigned long data)
  480. {
  481. struct fw_ohci *ohci = (struct fw_ohci *)data;
  482. int self_id_count, i, j, reg;
  483. int generation, new_generation;
  484. unsigned long flags;
  485. reg = reg_read(ohci, OHCI1394_NodeID);
  486. if (!(reg & OHCI1394_NodeID_idValid)) {
  487. fw_error("node ID not valid, new bus reset in progress\n");
  488. return;
  489. }
  490. ohci->node_id = reg & 0xffff;
  491. /* The count in the SelfIDCount register is the number of
  492. * bytes in the self ID receive buffer. Since we also receive
  493. * the inverted quadlets and a header quadlet, we shift one
  494. * bit extra to get the actual number of self IDs. */
  495. self_id_count = (reg_read(ohci, OHCI1394_SelfIDCount) >> 3) & 0x3ff;
  496. generation = (le32_to_cpu(ohci->self_id_cpu[0]) >> 16) & 0xff;
  497. for (i = 1, j = 0; j < self_id_count; i += 2, j++) {
  498. if (ohci->self_id_cpu[i] != ~ohci->self_id_cpu[i + 1])
  499. fw_error("inconsistent self IDs\n");
  500. ohci->self_id_buffer[j] = le32_to_cpu(ohci->self_id_cpu[i]);
  501. }
  502. /* Check the consistency of the self IDs we just read. The
  503. * problem we face is that a new bus reset can start while we
  504. * read out the self IDs from the DMA buffer. If this happens,
  505. * the DMA buffer will be overwritten with new self IDs and we
  506. * will read out inconsistent data. The OHCI specification
  507. * (section 11.2) recommends a technique similar to
  508. * linux/seqlock.h, where we remember the generation of the
  509. * self IDs in the buffer before reading them out and compare
  510. * it to the current generation after reading them out. If
  511. * the two generations match we know we have a consistent set
  512. * of self IDs. */
  513. new_generation = (reg_read(ohci, OHCI1394_SelfIDCount) >> 16) & 0xff;
  514. if (new_generation != generation) {
  515. fw_notify("recursive bus reset detected, "
  516. "discarding self ids\n");
  517. return;
  518. }
  519. /* FIXME: Document how the locking works. */
  520. spin_lock_irqsave(&ohci->lock, flags);
  521. ohci->generation = generation;
  522. at_context_stop(&ohci->at_request_ctx);
  523. at_context_stop(&ohci->at_response_ctx);
  524. reg_write(ohci, OHCI1394_IntEventClear, OHCI1394_busReset);
  525. /* This next bit is unrelated to the AT context stuff but we
  526. * have to do it under the spinlock also. If a new config rom
  527. * was set up before this reset, the old one is now no longer
  528. * in use and we can free it. Update the config rom pointers
  529. * to point to the current config rom and clear the
  530. * next_config_rom pointer so a new udpate can take place. */
  531. if (ohci->next_config_rom != NULL) {
  532. dma_free_coherent(ohci->card.device, CONFIG_ROM_SIZE,
  533. ohci->config_rom, ohci->config_rom_bus);
  534. ohci->config_rom = ohci->next_config_rom;
  535. ohci->config_rom_bus = ohci->next_config_rom_bus;
  536. ohci->next_config_rom = NULL;
  537. /* Restore config_rom image and manually update
  538. * config_rom registers. Writing the header quadlet
  539. * will indicate that the config rom is ready, so we
  540. * do that last. */
  541. reg_write(ohci, OHCI1394_BusOptions,
  542. be32_to_cpu(ohci->config_rom[2]));
  543. ohci->config_rom[0] = cpu_to_be32(ohci->next_header);
  544. reg_write(ohci, OHCI1394_ConfigROMhdr, ohci->next_header);
  545. }
  546. spin_unlock_irqrestore(&ohci->lock, flags);
  547. fw_core_handle_bus_reset(&ohci->card, ohci->node_id, generation,
  548. self_id_count, ohci->self_id_buffer);
  549. }
  550. static irqreturn_t irq_handler(int irq, void *data)
  551. {
  552. struct fw_ohci *ohci = data;
  553. u32 event, iso_event;
  554. int i;
  555. event = reg_read(ohci, OHCI1394_IntEventClear);
  556. if (!event)
  557. return IRQ_NONE;
  558. reg_write(ohci, OHCI1394_IntEventClear, event);
  559. if (event & OHCI1394_selfIDComplete)
  560. tasklet_schedule(&ohci->bus_reset_tasklet);
  561. if (event & OHCI1394_RQPkt)
  562. tasklet_schedule(&ohci->ar_request_ctx.tasklet);
  563. if (event & OHCI1394_RSPkt)
  564. tasklet_schedule(&ohci->ar_response_ctx.tasklet);
  565. if (event & OHCI1394_reqTxComplete)
  566. tasklet_schedule(&ohci->at_request_ctx.tasklet);
  567. if (event & OHCI1394_respTxComplete)
  568. tasklet_schedule(&ohci->at_response_ctx.tasklet);
  569. iso_event = reg_read(ohci, OHCI1394_IsoRecvIntEventSet);
  570. reg_write(ohci, OHCI1394_IsoRecvIntEventClear, iso_event);
  571. while (iso_event) {
  572. i = ffs(iso_event) - 1;
  573. tasklet_schedule(&ohci->ir_context_list[i].tasklet);
  574. iso_event &= ~(1 << i);
  575. }
  576. iso_event = reg_read(ohci, OHCI1394_IsoXmitIntEventSet);
  577. reg_write(ohci, OHCI1394_IsoXmitIntEventClear, iso_event);
  578. while (iso_event) {
  579. i = ffs(iso_event) - 1;
  580. tasklet_schedule(&ohci->it_context_list[i].tasklet);
  581. iso_event &= ~(1 << i);
  582. }
  583. return IRQ_HANDLED;
  584. }
  585. static int ohci_enable(struct fw_card *card, u32 *config_rom, size_t length)
  586. {
  587. struct fw_ohci *ohci = fw_ohci(card);
  588. struct pci_dev *dev = to_pci_dev(card->device);
  589. /* When the link is not yet enabled, the atomic config rom
  590. * update mechanism described below in ohci_set_config_rom()
  591. * is not active. We have to update ConfigRomHeader and
  592. * BusOptions manually, and the write to ConfigROMmap takes
  593. * effect immediately. We tie this to the enabling of the
  594. * link, so we have a valid config rom before enabling - the
  595. * OHCI requires that ConfigROMhdr and BusOptions have valid
  596. * values before enabling.
  597. *
  598. * However, when the ConfigROMmap is written, some controllers
  599. * always read back quadlets 0 and 2 from the config rom to
  600. * the ConfigRomHeader and BusOptions registers on bus reset.
  601. * They shouldn't do that in this initial case where the link
  602. * isn't enabled. This means we have to use the same
  603. * workaround here, setting the bus header to 0 and then write
  604. * the right values in the bus reset tasklet.
  605. */
  606. ohci->next_config_rom =
  607. dma_alloc_coherent(ohci->card.device, CONFIG_ROM_SIZE,
  608. &ohci->next_config_rom_bus, GFP_KERNEL);
  609. if (ohci->next_config_rom == NULL)
  610. return -ENOMEM;
  611. memset(ohci->next_config_rom, 0, CONFIG_ROM_SIZE);
  612. fw_memcpy_to_be32(ohci->next_config_rom, config_rom, length * 4);
  613. ohci->next_header = config_rom[0];
  614. ohci->next_config_rom[0] = 0;
  615. reg_write(ohci, OHCI1394_ConfigROMhdr, 0);
  616. reg_write(ohci, OHCI1394_BusOptions, config_rom[2]);
  617. reg_write(ohci, OHCI1394_ConfigROMmap, ohci->next_config_rom_bus);
  618. reg_write(ohci, OHCI1394_AsReqFilterHiSet, 0x80000000);
  619. if (request_irq(dev->irq, irq_handler,
  620. SA_SHIRQ, ohci_driver_name, ohci)) {
  621. fw_error("Failed to allocate shared interrupt %d.\n",
  622. dev->irq);
  623. dma_free_coherent(ohci->card.device, CONFIG_ROM_SIZE,
  624. ohci->config_rom, ohci->config_rom_bus);
  625. return -EIO;
  626. }
  627. reg_write(ohci, OHCI1394_HCControlSet,
  628. OHCI1394_HCControl_linkEnable |
  629. OHCI1394_HCControl_BIBimageValid);
  630. flush_writes(ohci);
  631. /* We are ready to go, initiate bus reset to finish the
  632. * initialization. */
  633. fw_core_initiate_bus_reset(&ohci->card, 1);
  634. return 0;
  635. }
  636. static int
  637. ohci_set_config_rom(struct fw_card *card, u32 *config_rom, size_t length)
  638. {
  639. struct fw_ohci *ohci;
  640. unsigned long flags;
  641. int retval = 0;
  642. __be32 *next_config_rom;
  643. dma_addr_t next_config_rom_bus;
  644. ohci = fw_ohci(card);
  645. /* When the OHCI controller is enabled, the config rom update
  646. * mechanism is a bit tricky, but easy enough to use. See
  647. * section 5.5.6 in the OHCI specification.
  648. *
  649. * The OHCI controller caches the new config rom address in a
  650. * shadow register (ConfigROMmapNext) and needs a bus reset
  651. * for the changes to take place. When the bus reset is
  652. * detected, the controller loads the new values for the
  653. * ConfigRomHeader and BusOptions registers from the specified
  654. * config rom and loads ConfigROMmap from the ConfigROMmapNext
  655. * shadow register. All automatically and atomically.
  656. *
  657. * Now, there's a twist to this story. The automatic load of
  658. * ConfigRomHeader and BusOptions doesn't honor the
  659. * noByteSwapData bit, so with a be32 config rom, the
  660. * controller will load be32 values in to these registers
  661. * during the atomic update, even on litte endian
  662. * architectures. The workaround we use is to put a 0 in the
  663. * header quadlet; 0 is endian agnostic and means that the
  664. * config rom isn't ready yet. In the bus reset tasklet we
  665. * then set up the real values for the two registers.
  666. *
  667. * We use ohci->lock to avoid racing with the code that sets
  668. * ohci->next_config_rom to NULL (see bus_reset_tasklet).
  669. */
  670. next_config_rom =
  671. dma_alloc_coherent(ohci->card.device, CONFIG_ROM_SIZE,
  672. &next_config_rom_bus, GFP_KERNEL);
  673. if (next_config_rom == NULL)
  674. return -ENOMEM;
  675. spin_lock_irqsave(&ohci->lock, flags);
  676. if (ohci->next_config_rom == NULL) {
  677. ohci->next_config_rom = next_config_rom;
  678. ohci->next_config_rom_bus = next_config_rom_bus;
  679. memset(ohci->next_config_rom, 0, CONFIG_ROM_SIZE);
  680. fw_memcpy_to_be32(ohci->next_config_rom, config_rom,
  681. length * 4);
  682. ohci->next_header = config_rom[0];
  683. ohci->next_config_rom[0] = 0;
  684. reg_write(ohci, OHCI1394_ConfigROMmap,
  685. ohci->next_config_rom_bus);
  686. } else {
  687. dma_free_coherent(ohci->card.device, CONFIG_ROM_SIZE,
  688. next_config_rom, next_config_rom_bus);
  689. retval = -EBUSY;
  690. }
  691. spin_unlock_irqrestore(&ohci->lock, flags);
  692. /* Now initiate a bus reset to have the changes take
  693. * effect. We clean up the old config rom memory and DMA
  694. * mappings in the bus reset tasklet, since the OHCI
  695. * controller could need to access it before the bus reset
  696. * takes effect. */
  697. if (retval == 0)
  698. fw_core_initiate_bus_reset(&ohci->card, 1);
  699. return retval;
  700. }
  701. static void ohci_send_request(struct fw_card *card, struct fw_packet *packet)
  702. {
  703. struct fw_ohci *ohci = fw_ohci(card);
  704. at_context_transmit(&ohci->at_request_ctx, packet);
  705. }
  706. static void ohci_send_response(struct fw_card *card, struct fw_packet *packet)
  707. {
  708. struct fw_ohci *ohci = fw_ohci(card);
  709. at_context_transmit(&ohci->at_response_ctx, packet);
  710. }
  711. static int
  712. ohci_enable_phys_dma(struct fw_card *card, int node_id, int generation)
  713. {
  714. struct fw_ohci *ohci = fw_ohci(card);
  715. unsigned long flags;
  716. int n, retval = 0;
  717. /* FIXME: Make sure this bitmask is cleared when we clear the busReset
  718. * interrupt bit. Clear physReqResourceAllBuses on bus reset. */
  719. spin_lock_irqsave(&ohci->lock, flags);
  720. if (ohci->generation != generation) {
  721. retval = -ESTALE;
  722. goto out;
  723. }
  724. /* NOTE, if the node ID contains a non-local bus ID, physical DMA is
  725. * enabled for _all_ nodes on remote buses. */
  726. n = (node_id & 0xffc0) == LOCAL_BUS ? node_id & 0x3f : 63;
  727. if (n < 32)
  728. reg_write(ohci, OHCI1394_PhyReqFilterLoSet, 1 << n);
  729. else
  730. reg_write(ohci, OHCI1394_PhyReqFilterHiSet, 1 << (n - 32));
  731. flush_writes(ohci);
  732. out:
  733. spin_unlock_irqrestore(&ohci->lock, flags);
  734. return retval;
  735. }
  736. static void ir_context_tasklet(unsigned long data)
  737. {
  738. struct iso_context *ctx = (struct iso_context *)data;
  739. (void)ctx;
  740. }
  741. #define ISO_BUFFER_SIZE (64 * 1024)
  742. static void flush_iso_context(struct iso_context *ctx)
  743. {
  744. struct fw_ohci *ohci = fw_ohci(ctx->base.card);
  745. struct descriptor *d, *last;
  746. u32 address;
  747. int z;
  748. dma_sync_single_for_cpu(ohci->card.device, ctx->buffer_bus,
  749. ISO_BUFFER_SIZE, DMA_TO_DEVICE);
  750. d = ctx->tail_descriptor;
  751. last = ctx->tail_descriptor_last;
  752. while (last->branch_address != 0 && last->transfer_status != 0) {
  753. address = le32_to_cpu(last->branch_address);
  754. z = address & 0xf;
  755. d = ctx->buffer + (address - ctx->buffer_bus) / sizeof *d;
  756. if (z == 2)
  757. last = d;
  758. else
  759. last = d + z - 1;
  760. if (le16_to_cpu(last->control) & descriptor_irq_always)
  761. ctx->base.callback(&ctx->base,
  762. 0, le16_to_cpu(last->res_count),
  763. ctx->base.callback_data);
  764. }
  765. ctx->tail_descriptor = d;
  766. ctx->tail_descriptor_last = last;
  767. }
  768. static void it_context_tasklet(unsigned long data)
  769. {
  770. struct iso_context *ctx = (struct iso_context *)data;
  771. flush_iso_context(ctx);
  772. }
  773. static struct fw_iso_context *ohci_allocate_iso_context(struct fw_card *card,
  774. int type)
  775. {
  776. struct fw_ohci *ohci = fw_ohci(card);
  777. struct iso_context *ctx, *list;
  778. void (*tasklet) (unsigned long data);
  779. u32 *mask;
  780. unsigned long flags;
  781. int index;
  782. if (type == FW_ISO_CONTEXT_TRANSMIT) {
  783. mask = &ohci->it_context_mask;
  784. list = ohci->it_context_list;
  785. tasklet = it_context_tasklet;
  786. } else {
  787. mask = &ohci->ir_context_mask;
  788. list = ohci->ir_context_list;
  789. tasklet = ir_context_tasklet;
  790. }
  791. spin_lock_irqsave(&ohci->lock, flags);
  792. index = ffs(*mask) - 1;
  793. if (index >= 0)
  794. *mask &= ~(1 << index);
  795. spin_unlock_irqrestore(&ohci->lock, flags);
  796. if (index < 0)
  797. return ERR_PTR(-EBUSY);
  798. ctx = &list[index];
  799. memset(ctx, 0, sizeof *ctx);
  800. tasklet_init(&ctx->tasklet, tasklet, (unsigned long)ctx);
  801. ctx->buffer = kmalloc(ISO_BUFFER_SIZE, GFP_KERNEL);
  802. if (ctx->buffer == NULL) {
  803. spin_lock_irqsave(&ohci->lock, flags);
  804. *mask |= 1 << index;
  805. spin_unlock_irqrestore(&ohci->lock, flags);
  806. return ERR_PTR(-ENOMEM);
  807. }
  808. ctx->buffer_bus =
  809. dma_map_single(card->device, ctx->buffer,
  810. ISO_BUFFER_SIZE, DMA_TO_DEVICE);
  811. ctx->head_descriptor = ctx->buffer;
  812. ctx->prev_descriptor = ctx->buffer;
  813. ctx->tail_descriptor = ctx->buffer;
  814. ctx->tail_descriptor_last = ctx->buffer;
  815. /* We put a dummy descriptor in the buffer that has a NULL
  816. * branch address and looks like it's been sent. That way we
  817. * have a descriptor to append DMA programs to. Also, the
  818. * ring buffer invariant is that it always has at least one
  819. * element so that head == tail means buffer full. */
  820. memset(ctx->head_descriptor, 0, sizeof *ctx->head_descriptor);
  821. ctx->head_descriptor->control = cpu_to_le16(descriptor_output_last);
  822. ctx->head_descriptor->transfer_status = cpu_to_le16(0x8011);
  823. ctx->head_descriptor++;
  824. return &ctx->base;
  825. }
  826. static int ohci_send_iso(struct fw_iso_context *base, s32 cycle)
  827. {
  828. struct iso_context *ctx = (struct iso_context *)base;
  829. struct fw_ohci *ohci = fw_ohci(ctx->base.card);
  830. u32 cycle_match = 0;
  831. int index;
  832. index = ctx - ohci->it_context_list;
  833. if (cycle > 0)
  834. cycle_match = CONTEXT_CYCLE_MATCH_ENABLE |
  835. (cycle & 0x7fff) << 16;
  836. reg_write(ohci, OHCI1394_IsoXmitIntMaskSet, 1 << index);
  837. reg_write(ohci, OHCI1394_IsoXmitCommandPtr(index),
  838. le32_to_cpu(ctx->tail_descriptor_last->branch_address));
  839. reg_write(ohci, OHCI1394_IsoXmitContextControlClear(index), ~0);
  840. reg_write(ohci, OHCI1394_IsoXmitContextControlSet(index),
  841. CONTEXT_RUN | cycle_match);
  842. flush_writes(ohci);
  843. return 0;
  844. }
  845. static void ohci_free_iso_context(struct fw_iso_context *base)
  846. {
  847. struct fw_ohci *ohci = fw_ohci(base->card);
  848. struct iso_context *ctx = (struct iso_context *)base;
  849. unsigned long flags;
  850. int index;
  851. flush_iso_context(ctx);
  852. spin_lock_irqsave(&ohci->lock, flags);
  853. if (ctx->base.type == FW_ISO_CONTEXT_TRANSMIT) {
  854. index = ctx - ohci->it_context_list;
  855. reg_write(ohci, OHCI1394_IsoXmitContextControlClear(index), ~0);
  856. reg_write(ohci, OHCI1394_IsoXmitIntMaskClear, 1 << index);
  857. ohci->it_context_mask |= 1 << index;
  858. } else {
  859. index = ctx - ohci->ir_context_list;
  860. reg_write(ohci, OHCI1394_IsoRcvContextControlClear(index), ~0);
  861. reg_write(ohci, OHCI1394_IsoRecvIntMaskClear, 1 << index);
  862. ohci->ir_context_mask |= 1 << index;
  863. }
  864. flush_writes(ohci);
  865. dma_unmap_single(ohci->card.device, ctx->buffer_bus,
  866. ISO_BUFFER_SIZE, DMA_TO_DEVICE);
  867. spin_unlock_irqrestore(&ohci->lock, flags);
  868. }
  869. static int
  870. ohci_queue_iso(struct fw_iso_context *base,
  871. struct fw_iso_packet *packet, void *payload)
  872. {
  873. struct iso_context *ctx = (struct iso_context *)base;
  874. struct fw_ohci *ohci = fw_ohci(ctx->base.card);
  875. struct descriptor *d, *end, *last, *tail, *pd;
  876. struct fw_iso_packet *p;
  877. __le32 *header;
  878. dma_addr_t d_bus;
  879. u32 z, header_z, payload_z, irq;
  880. u32 payload_index, payload_end_index, next_page_index;
  881. int index, page, end_page, i, length, offset;
  882. /* FIXME: Cycle lost behavior should be configurable: lose
  883. * packet, retransmit or terminate.. */
  884. p = packet;
  885. payload_index = payload - ctx->base.buffer;
  886. d = ctx->head_descriptor;
  887. tail = ctx->tail_descriptor;
  888. end = ctx->buffer + ISO_BUFFER_SIZE / sizeof(struct descriptor);
  889. if (p->skip)
  890. z = 1;
  891. else
  892. z = 2;
  893. if (p->header_length > 0)
  894. z++;
  895. /* Determine the first page the payload isn't contained in. */
  896. end_page = PAGE_ALIGN(payload_index + p->payload_length) >> PAGE_SHIFT;
  897. if (p->payload_length > 0)
  898. payload_z = end_page - (payload_index >> PAGE_SHIFT);
  899. else
  900. payload_z = 0;
  901. z += payload_z;
  902. /* Get header size in number of descriptors. */
  903. header_z = DIV_ROUND_UP(p->header_length, sizeof *d);
  904. if (d + z + header_z <= tail) {
  905. goto has_space;
  906. } else if (d > tail && d + z + header_z <= end) {
  907. goto has_space;
  908. } else if (d > tail && ctx->buffer + z + header_z <= tail) {
  909. d = ctx->buffer;
  910. goto has_space;
  911. }
  912. /* No space in buffer */
  913. return -1;
  914. has_space:
  915. memset(d, 0, (z + header_z) * sizeof *d);
  916. d_bus = ctx->buffer_bus + (d - ctx->buffer) * sizeof *d;
  917. if (!p->skip) {
  918. d[0].control = cpu_to_le16(descriptor_key_immediate);
  919. d[0].req_count = cpu_to_le16(8);
  920. header = (__le32 *) &d[1];
  921. header[0] = cpu_to_le32(it_header_sy(p->sy) |
  922. it_header_tag(p->tag) |
  923. it_header_tcode(TCODE_STREAM_DATA) |
  924. it_header_channel(ctx->base.channel) |
  925. it_header_speed(ctx->base.speed));
  926. header[1] =
  927. cpu_to_le32(it_header_data_length(p->header_length +
  928. p->payload_length));
  929. }
  930. if (p->header_length > 0) {
  931. d[2].req_count = cpu_to_le16(p->header_length);
  932. d[2].data_address = cpu_to_le32(d_bus + z * sizeof *d);
  933. memcpy(&d[z], p->header, p->header_length);
  934. }
  935. pd = d + z - payload_z;
  936. payload_end_index = payload_index + p->payload_length;
  937. for (i = 0; i < payload_z; i++) {
  938. page = payload_index >> PAGE_SHIFT;
  939. offset = payload_index & ~PAGE_MASK;
  940. next_page_index = (page + 1) << PAGE_SHIFT;
  941. length =
  942. min(next_page_index, payload_end_index) - payload_index;
  943. pd[i].req_count = cpu_to_le16(length);
  944. pd[i].data_address = cpu_to_le32(ctx->base.pages[page] + offset);
  945. payload_index += length;
  946. }
  947. if (z == 2)
  948. last = d;
  949. else
  950. last = d + z - 1;
  951. if (p->interrupt)
  952. irq = descriptor_irq_always;
  953. else
  954. irq = descriptor_no_irq;
  955. last->control = cpu_to_le16(descriptor_output_last |
  956. descriptor_status |
  957. descriptor_branch_always |
  958. irq);
  959. dma_sync_single_for_device(ohci->card.device, ctx->buffer_bus,
  960. ISO_BUFFER_SIZE, DMA_TO_DEVICE);
  961. ctx->head_descriptor = d + z + header_z;
  962. ctx->prev_descriptor->branch_address = cpu_to_le32(d_bus | z);
  963. ctx->prev_descriptor = last;
  964. index = ctx - ohci->it_context_list;
  965. reg_write(ohci, OHCI1394_IsoXmitContextControlSet(index), CONTEXT_WAKE);
  966. flush_writes(ohci);
  967. return 0;
  968. }
  969. static const struct fw_card_driver ohci_driver = {
  970. .name = ohci_driver_name,
  971. .enable = ohci_enable,
  972. .update_phy_reg = ohci_update_phy_reg,
  973. .set_config_rom = ohci_set_config_rom,
  974. .send_request = ohci_send_request,
  975. .send_response = ohci_send_response,
  976. .enable_phys_dma = ohci_enable_phys_dma,
  977. .allocate_iso_context = ohci_allocate_iso_context,
  978. .free_iso_context = ohci_free_iso_context,
  979. .queue_iso = ohci_queue_iso,
  980. .send_iso = ohci_send_iso,
  981. };
  982. static int software_reset(struct fw_ohci *ohci)
  983. {
  984. int i;
  985. reg_write(ohci, OHCI1394_HCControlSet, OHCI1394_HCControl_softReset);
  986. for (i = 0; i < OHCI_LOOP_COUNT; i++) {
  987. if ((reg_read(ohci, OHCI1394_HCControlSet) &
  988. OHCI1394_HCControl_softReset) == 0)
  989. return 0;
  990. msleep(1);
  991. }
  992. return -EBUSY;
  993. }
  994. /* ---------- pci subsystem interface ---------- */
  995. enum {
  996. CLEANUP_SELF_ID,
  997. CLEANUP_REGISTERS,
  998. CLEANUP_IOMEM,
  999. CLEANUP_DISABLE,
  1000. CLEANUP_PUT_CARD,
  1001. };
  1002. static int cleanup(struct fw_ohci *ohci, int stage, int code)
  1003. {
  1004. struct pci_dev *dev = to_pci_dev(ohci->card.device);
  1005. switch (stage) {
  1006. case CLEANUP_SELF_ID:
  1007. dma_free_coherent(ohci->card.device, SELF_ID_BUF_SIZE,
  1008. ohci->self_id_cpu, ohci->self_id_bus);
  1009. case CLEANUP_REGISTERS:
  1010. kfree(ohci->it_context_list);
  1011. kfree(ohci->ir_context_list);
  1012. pci_iounmap(dev, ohci->registers);
  1013. case CLEANUP_IOMEM:
  1014. pci_release_region(dev, 0);
  1015. case CLEANUP_DISABLE:
  1016. pci_disable_device(dev);
  1017. case CLEANUP_PUT_CARD:
  1018. fw_card_put(&ohci->card);
  1019. }
  1020. return code;
  1021. }
  1022. static int __devinit
  1023. pci_probe(struct pci_dev *dev, const struct pci_device_id *ent)
  1024. {
  1025. struct fw_ohci *ohci;
  1026. u32 bus_options, max_receive, link_speed;
  1027. u64 guid;
  1028. int error_code;
  1029. size_t size;
  1030. ohci = kzalloc(sizeof *ohci, GFP_KERNEL);
  1031. if (ohci == NULL) {
  1032. fw_error("Could not malloc fw_ohci data.\n");
  1033. return -ENOMEM;
  1034. }
  1035. fw_card_initialize(&ohci->card, &ohci_driver, &dev->dev);
  1036. if (pci_enable_device(dev)) {
  1037. fw_error("Failed to enable OHCI hardware.\n");
  1038. return cleanup(ohci, CLEANUP_PUT_CARD, -ENODEV);
  1039. }
  1040. pci_set_master(dev);
  1041. pci_write_config_dword(dev, OHCI1394_PCI_HCI_Control, 0);
  1042. pci_set_drvdata(dev, ohci);
  1043. spin_lock_init(&ohci->lock);
  1044. tasklet_init(&ohci->bus_reset_tasklet,
  1045. bus_reset_tasklet, (unsigned long)ohci);
  1046. if (pci_request_region(dev, 0, ohci_driver_name)) {
  1047. fw_error("MMIO resource unavailable\n");
  1048. return cleanup(ohci, CLEANUP_DISABLE, -EBUSY);
  1049. }
  1050. ohci->registers = pci_iomap(dev, 0, OHCI1394_REGISTER_SIZE);
  1051. if (ohci->registers == NULL) {
  1052. fw_error("Failed to remap registers\n");
  1053. return cleanup(ohci, CLEANUP_IOMEM, -ENXIO);
  1054. }
  1055. if (software_reset(ohci)) {
  1056. fw_error("Failed to reset ohci card.\n");
  1057. return cleanup(ohci, CLEANUP_REGISTERS, -EBUSY);
  1058. }
  1059. /* Now enable LPS, which we need in order to start accessing
  1060. * most of the registers. In fact, on some cards (ALI M5251),
  1061. * accessing registers in the SClk domain without LPS enabled
  1062. * will lock up the machine. Wait 50msec to make sure we have
  1063. * full link enabled. */
  1064. reg_write(ohci, OHCI1394_HCControlSet,
  1065. OHCI1394_HCControl_LPS |
  1066. OHCI1394_HCControl_postedWriteEnable);
  1067. flush_writes(ohci);
  1068. msleep(50);
  1069. reg_write(ohci, OHCI1394_HCControlClear,
  1070. OHCI1394_HCControl_noByteSwapData);
  1071. reg_write(ohci, OHCI1394_LinkControlSet,
  1072. OHCI1394_LinkControl_rcvSelfID |
  1073. OHCI1394_LinkControl_cycleTimerEnable |
  1074. OHCI1394_LinkControl_cycleMaster);
  1075. ar_context_init(&ohci->ar_request_ctx, ohci,
  1076. OHCI1394_AsReqRcvContextControlSet);
  1077. ar_context_init(&ohci->ar_response_ctx, ohci,
  1078. OHCI1394_AsRspRcvContextControlSet);
  1079. at_context_init(&ohci->at_request_ctx, ohci,
  1080. OHCI1394_AsReqTrContextControlSet);
  1081. at_context_init(&ohci->at_response_ctx, ohci,
  1082. OHCI1394_AsRspTrContextControlSet);
  1083. reg_write(ohci, OHCI1394_ATRetries,
  1084. OHCI1394_MAX_AT_REQ_RETRIES |
  1085. (OHCI1394_MAX_AT_RESP_RETRIES << 4) |
  1086. (OHCI1394_MAX_PHYS_RESP_RETRIES << 8));
  1087. reg_write(ohci, OHCI1394_IsoRecvIntMaskSet, ~0);
  1088. ohci->it_context_mask = reg_read(ohci, OHCI1394_IsoRecvIntMaskSet);
  1089. reg_write(ohci, OHCI1394_IsoRecvIntMaskClear, ~0);
  1090. size = sizeof(struct iso_context) * hweight32(ohci->it_context_mask);
  1091. ohci->it_context_list = kzalloc(size, GFP_KERNEL);
  1092. reg_write(ohci, OHCI1394_IsoXmitIntMaskSet, ~0);
  1093. ohci->ir_context_mask = reg_read(ohci, OHCI1394_IsoXmitIntMaskSet);
  1094. reg_write(ohci, OHCI1394_IsoXmitIntMaskClear, ~0);
  1095. size = sizeof(struct iso_context) * hweight32(ohci->ir_context_mask);
  1096. ohci->ir_context_list = kzalloc(size, GFP_KERNEL);
  1097. if (ohci->it_context_list == NULL || ohci->ir_context_list == NULL) {
  1098. fw_error("Out of memory for it/ir contexts.\n");
  1099. return cleanup(ohci, CLEANUP_REGISTERS, -ENOMEM);
  1100. }
  1101. /* self-id dma buffer allocation */
  1102. ohci->self_id_cpu = dma_alloc_coherent(ohci->card.device,
  1103. SELF_ID_BUF_SIZE,
  1104. &ohci->self_id_bus,
  1105. GFP_KERNEL);
  1106. if (ohci->self_id_cpu == NULL) {
  1107. fw_error("Out of memory for self ID buffer.\n");
  1108. return cleanup(ohci, CLEANUP_REGISTERS, -ENOMEM);
  1109. }
  1110. reg_write(ohci, OHCI1394_SelfIDBuffer, ohci->self_id_bus);
  1111. reg_write(ohci, OHCI1394_PhyUpperBound, 0x00010000);
  1112. reg_write(ohci, OHCI1394_IntEventClear, ~0);
  1113. reg_write(ohci, OHCI1394_IntMaskClear, ~0);
  1114. reg_write(ohci, OHCI1394_IntMaskSet,
  1115. OHCI1394_selfIDComplete |
  1116. OHCI1394_RQPkt | OHCI1394_RSPkt |
  1117. OHCI1394_reqTxComplete | OHCI1394_respTxComplete |
  1118. OHCI1394_isochRx | OHCI1394_isochTx |
  1119. OHCI1394_masterIntEnable);
  1120. bus_options = reg_read(ohci, OHCI1394_BusOptions);
  1121. max_receive = (bus_options >> 12) & 0xf;
  1122. link_speed = bus_options & 0x7;
  1123. guid = ((u64) reg_read(ohci, OHCI1394_GUIDHi) << 32) |
  1124. reg_read(ohci, OHCI1394_GUIDLo);
  1125. error_code = fw_card_add(&ohci->card, max_receive, link_speed, guid);
  1126. if (error_code < 0)
  1127. return cleanup(ohci, CLEANUP_SELF_ID, error_code);
  1128. fw_notify("Added fw-ohci device %s.\n", dev->dev.bus_id);
  1129. return 0;
  1130. }
  1131. static void pci_remove(struct pci_dev *dev)
  1132. {
  1133. struct fw_ohci *ohci;
  1134. ohci = pci_get_drvdata(dev);
  1135. reg_write(ohci, OHCI1394_IntMaskClear, OHCI1394_masterIntEnable);
  1136. fw_core_remove_card(&ohci->card);
  1137. /* FIXME: Fail all pending packets here, now that the upper
  1138. * layers can't queue any more. */
  1139. software_reset(ohci);
  1140. free_irq(dev->irq, ohci);
  1141. cleanup(ohci, CLEANUP_SELF_ID, 0);
  1142. fw_notify("Removed fw-ohci device.\n");
  1143. }
  1144. static struct pci_device_id pci_table[] = {
  1145. { PCI_DEVICE_CLASS(PCI_CLASS_SERIAL_FIREWIRE_OHCI, ~0) },
  1146. { }
  1147. };
  1148. MODULE_DEVICE_TABLE(pci, pci_table);
  1149. static struct pci_driver fw_ohci_pci_driver = {
  1150. .name = ohci_driver_name,
  1151. .id_table = pci_table,
  1152. .probe = pci_probe,
  1153. .remove = pci_remove,
  1154. };
  1155. MODULE_AUTHOR("Kristian Hoegsberg <krh@bitplanet.net>");
  1156. MODULE_DESCRIPTION("Driver for PCI OHCI IEEE1394 controllers");
  1157. MODULE_LICENSE("GPL");
  1158. static int __init fw_ohci_init(void)
  1159. {
  1160. return pci_register_driver(&fw_ohci_pci_driver);
  1161. }
  1162. static void __exit fw_ohci_cleanup(void)
  1163. {
  1164. pci_unregister_driver(&fw_ohci_pci_driver);
  1165. }
  1166. module_init(fw_ohci_init);
  1167. module_exit(fw_ohci_cleanup);